Re: A Friday Regex Head Scratcher

"Dave O'Neill" <[email protected]>
Newsgroups gmane.user-groups.linux.ottawa.general
Message-ID <[email protected]>
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.

Cheers,
Dave
-- 
Dave O'Neill <[email protected]>    Roaring Penguin Software Inc.
+1 (613) 231-6599                        http://www.roaringpenguin.com/
For CanIt technical support, please mail: [email protected]
-- 
OCLUG general discussion list
[email protected]
http://oclug.on.ca/mailman/listinfo/oclug
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.