开发者

perl + how to use chomp in foreach loop

the following perl script (Showing down)

print the following XML file but the XML have problem because the unnecessary "&#10" char after TEST word

 <ROOT>
     <FILE Name="TEST1&#10;"/>
     <FILE Name="TEST2&#10;"/>
     <FILE Name="TEST&#10;"/>ar
</ROOT>

I think the "&#10" characters its because the linebreak (read the output of a command into @myNames with the backtick operator) it seems that this problem of existing "&#10" in my XML can be solved chom开发者_如何学运维p command

my question: in which way need to add the chomp command in my script in order to solve the problem

my perl script

#!/usr/bin/perl

use warnings;
use XML::LibXML;


my $doc  = XML::LibXML::Document->new; 
my $root = $doc->createElement('ROOT');
$doc->setDocumentElement($root);


my @myNames=` ls /var/tmp | grep FILE.xml | sed "s/.FILE.*//"  `;

foreach (@myNames) {

my $objVar = $doc->createElement('FILE');
$root->appendChild($objVar);
$objVar->setAttribute('Name' , $_ );

} 


my @lines = split /\n/, $doc->toString(1);
shift @lines;

foreach (@lines) {

print "$_\n";
}

the files under /var/tmp

   TEST1-FILE.xml
   TEST2-FILE.xml
   TEST-FILE.xml


Go ahead and use it immediately after you get the list of filenames:

my @myNames=` ls /var/tmp | grep FILE.xml | sed "s/.FILE.*//"  `;
chomp(@myNames);
...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜