简体   繁体   中英

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 chomp 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);
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM