0

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);
1
  • It may be far easier to use xml decoding/encoding modules like XML::Simple – mveroone Nov 27 '13 at 10:12
0

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";

        }

}
3
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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