If the regexen are mutually exclusive (ie, they do not affect the same things), then perhaps something along the lines of the following:
my %regexen = (
'*' => [
{ 'replace' => qr/blah/, 'with' => 'duh' },
{ 'replace' => qr/baz/, 'with' => 'quuz' },
{ 'replace' => qr/(b)ar/, 'with' => '$1um' }
],
'cgi-bin' => [
{
'replace' => qr!usr/local/bin/perl!,
'with' => 'usr/bin/perl'
}
]
);
foreach my $directory ( '*', 'cgi-bin' ) {
foreach my $file ( glob("$directory/*") ) {
next
if (
$file =~ m/\.(jpg|png|gif|zip|gz|jar|pl|bak|\~)$/i );
print "Processing... $file (old file as ${file}.bak\n";
open( INF, $file )
or die("Can't open $file for input: $!\n");
open( OUTF, '>' . $file . '.new' )
or die("Can't open $file.new for output: $!\n");
binmode(INF);
binmode(OUTF);
while ( my $line = <INF> ) {
foreach my $rx ( @{ $regexen{$directory} } ) {
$line =~ s/$rx->{'replace'}/$rx->{'with'}/;
}
print OUTF $line;
}
close(INF);
close(OUTF);
rename( $file, $file . '.bak' );
rename( $file . '.new', $file );
}
}
This would loop thru a directory at a time, making changes to each file (and saving the original with a .bak extension). The only problem with the code above is if the modifications affect the same things, in which case if all modifications affect all files, you would add them under the '*' set, otherwise you would have to add them to the set for each of the directories (or files, if you need to break it down in that way).
I'm not sure if this is necessarily the best way, but it is an idea. Hope it helps.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.