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, 2013 at 10:12

1 Answer 1

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" Nov 27, 2013 at 9:33
  • there were small mistake in if statement!!!i'have correct that one...try this now!!! Nov 27, 2013 at 9:57
  • this time it should work! Nov 27, 2013 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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