I have tried to put the script within a foreach loop, but it did not work.
So, I'm guessing that you didn't try it this way:
#!/usr/bin/perl
use strict;
use warnings;
for my $f ( @ARGV ) {
local $/;
open( I, '<', $f );
open( O, '>', "$f.bak" );
my $count = 0;
my $line = <I>;
$line =~ s{ (<\/div>) }
{ if (++$count == 2){
"\t<?php include(\$_SERVER['DOCUMENT_ROOT'].\"\/incl
+udes\/footer.php\"); ?>\n\n".$1;
} else {
$1;
}
}gex;
print O $line;
}
That works for me. (BTW, I'm compulsive about making the indentation look right -- seems silly, but it's really helpful to keep code less illegible.)
If you have so many files that you can't fit them all as args on a command line, there's the unix "xargs" tool:
ls | xargs your_prog ## or use "find ... | xargs your_prog"