This is my take at it:
use warnings;
use strict;
my @repls = (
[ qr/pat1/, 'repl1' ],
[ qr/pat2/, 'repl2' ],
[ qr/pat3/, 'repl3' ],
);
my $txt = <<'EOF';
This is some example (pat1) text which
will be (pat3) modified according (pat2)
to some replacement (pat2) rules.
EOF
foreach my $r (@repls) {
$txt =~ s/$r->[0]/$r->[1]/eg;
}
print $txt;
__END__
This is some example (repl1) text which
will be (repl3) modified according (repl2)
to some replacement (repl2) rules.
If you want to capture things and use them, it gets more (too?) complicated:
use warnings;
use strict;
my @repls = (
[ qr/pat(\d+)/, 'qq{repl$1}' ],
);
my $txt = <<'EOF';
This is some example (pat42) text
EOF
foreach my $r (@repls) {
$txt =~ s/$r->[0]/$r->[1]/eeg;
}
print $txt;
__END__
This is some example (repl42) text
Note the use of qq{} in single quotes and the double eval in the substitution. The first eval turns $r->[1] into qq{repl$1} and the second one turns that into repl42.
--
David Serrano
(Please treat my english text just like Perl code, i.e. feel free to notify me of any syntax, grammar, style and/or spelling errors. Thank you!).
-
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.
|