Search code examples
perlchomp

chomp() usage in Perl


I have an XML file. There is some blank line in the file. How can I remove only the blank lines? I tried chomp() method and found that it will remove all the new line symbols. I still need to output the standard XML file format.

while(my $line = <$fh>) {

        #chomp($line);
        #$line =~ s/\s+//g;
        print $line;
}

__DATA__
    <item>

      <key>AB</key>

      <info>

        <format>10</format>

        <description>Any binary string</description>

        <value>NA</value>

        <whereUsed>A2B25,A2B26</whereUsed>

      </info>

    </item>

The output form below expected.

<item>
  <key>AB</key>
  <info>
    <format>10</format>
    <description>Any binary string</description>
    <value>NA</value>
    <whereUsed>A2B25,A2B26</whereUsed>
  </info>
</item>

Solution

  • In your loop, before the print:

    next unless $line =~ /\S/;