note
Tanktalus
<p>No doubt - advantage for compiling the regular expressions only once. But I'd take it just a tiny bit further - instead of all the copying around of the line:
<code>
#list of regex-strings
my @regex =
(
's/(a+)/\U$1/g',
's/([bz]+)/XX/g',
);
#is now a list of subroutines
@regex = map { eval "sub { $_ }" } @regex;
</code>
Notes: got rid of the copying of the line in and out, we'll just work on the global $_; also got rid of the extraneous assignment to the global $sub variable. Now you use it like:
<code>
my @text =
(
"aaaabbzz",
"bbbyyy",
);
for ( @text )
{
print "org $_\n";
for my $re ( @regex )
{
&$re();
# or even just &$re
}
print "new $_\n";
}
</code>
The advantage here is when you have many regex's (which the OP said they would) - less copying of data around. It's just a tiny bit more dangerous since so many functions modify $_, though.</p>
429692
429751