the following perl script (Showing down)
print the following XML file but the XML have problem because the unnecessary "
" char after TEST word
<ROOT>
<FILE Name="TEST1 "/>
<FILE Name="TEST2 "/>
<FILE Name="TEST "/>ar
</ROOT>
I think the "
" characters its because the linebreak (read the output of a command into @myNames with the backtick operator) it seems that this problem of existing "
" 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);
...
精彩评论