[oclug] A Friday Regex Head Scratcher
dmo at dmo.ca
Mon Apr 20 14:26:37 EDT 2009
Jon Earle wrote:
> I was just reading up on the $& stuff, and according to Programming Perl, 3rd
> Ed, pg 146, last para:
> "Perl uses a similar mechanism to produce $1, $2 and so on, so you also pay a
> price for each pattern that contains capturing parentheses."
Similar, but not the same. The overhead for numbered captures is only
incurred on the regexes that use capturing parentheses.
The difference with $& is that if you reference the $& variable (or $'
or $`) even once, anywhere in your code, Perl needs to capture and save
them for every regular expression executed in that process, not just the
one for which you expected to use the value. This is the extra penalty
you want to avoid.
This generally won't be a problem for quickie commandline scripts that
only run one or two regexes, but if you're writing something that
performs a lot of regex matching, or that runs as a daemon, or under
mod_perl or fastcgi, you should avoid using $&, $' and $`. Or... if you
really need them and are on Perl 5.10, use the newer equivalents that
don't come with a global penalty. See "man perlre" for more details.
Dave O'Neill <dmo at roaringpenguin.com> Roaring Penguin Software Inc.
+1 (613) 231-6599 http://www.roaringpenguin.com/
For CanIt technical support, please mail: support at roaringpenguin.com
More information about the OCLUG