在Perl中使用chomp()

Nan*_* HE 1 perl chomp

我有一个XML文件.文件中有一些空白行.如何只删除空白行?我尝试了chomp()方法,发现它将删除所有新的行符号.我仍然需要输出标准的XML文件格式.

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>
Run Code Online (Sandbox Code Playgroud)

预期的输出形式.

<item>
  <key>AB</key>
  <info>
    <format>10</format>
    <description>Any binary string</description>
    <value>NA</value>
    <whereUsed>A2B25,A2B26</whereUsed>
  </info>
</item>
Run Code Online (Sandbox Code Playgroud)

yst*_*sth 8

在你的循环中,在打印之前:

next unless $line =~ /\S/;
Run Code Online (Sandbox Code Playgroud)