Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have written a perl script to modify xml file which is working fine for only one file. I want my script to fetch the xml folder and modify the xml file inside it. Like that it should run for all the xml files in the xml folders. so how can i achieve it.

Here is my code.

open(FILE, "/home/AP/abc.xml") || die "File not found";
my @lines = <FILE>;
close(FILE);

my @newlines;
foreach(@lines) {
   $_ =~ s/<abc>/$&\n<!--a-->\n<!--b-->\n<!--c-->/g ;
                     s/hai/bye/g;
                     s/---/--/g;
                     s/***/**/g;

   push(@newlines,$_);
}
open(FILE, "/home/AP/abc.xml") || die "File not found";
print FILE  @newlines;
close(FILE);
share|improve this question
    
It may be far easier to use xml decoding/encoding modules like XML::Simple –  Kwaio Nov 27 '13 at 10:12
add comment

1 Answer

Actually you can achive this in many ways.....just try the following code and let me know if any clarification or got any error.

sub fileprocessor{
    (my $file_name)=@_;

    my $tmp_filename="/home/AP/$file_name";
open(FILE, "$tmp_filename") || die "File not found";
my @lines = <FILE>;
close(FILE);

my @newlines;
foreach(@lines) {
   $_ =~ s/<abc>/$&\n<!--a-->\n<!--b-->\n<!--c-->/g ;
                     s/hai/bye/g;
                     s/---/--/g;
                     s/***/**/g;

   push(@newlines,$_);
}
open(FILE, "$tmp_filename") || die "File not found";
print FILE  @newlines;
close(FILE);

}


foreach(<*>){

     if(-f $_){
                fileprocessor($_);
        }else{

            print "directory\n";

        }

}
share|improve this answer
    
Its giving syntax error at near "-f if" near "}else" –  user1345837 Nov 27 '13 at 9:33
    
there were small mistake in if statement!!!i'have correct that one...try this now!!! –  Thiyagu ATR Nov 27 '13 at 9:57
    
this time it should work! –  Thiyagu ATR Nov 27 '13 at 9:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.