[Melbourne-pm] An old one but an important one

Tim Connors tconnors at astro.swin.edu.au
Sun Jan 20 22:26:11 PST 2008


On Mon, 21 Jan 2008, Jacinta Richardson wrote:

> Tim Connors wrote:
> > Crap.  I was always under the impression that $& $' and $` were esoteric 
> > things that are only used by perlre gurus.
> > 
> > But perlre(1) tells me that parentheses and $1, $2,... use them internally 
> > with the huge performance hit, but (at least) don't come with the cost of 
> > slowing down all uses of REs.
> > 
> > I wonder how much of my own code uses parentheses avoidably?
> 
> Probably a lot.  The  (?:  construct is ugly and I sometimes I don't remember
> which order the : and ? go in.  Still, although it's slower than it needs to be,
> it doesn't have the same performance impact on _every_ other regular expression
> in your program.  Using $&, $' or $` does affect every regular expression,
> because Perl cannot tell in advance (at compile time) which regular expression
> will set $& etc before its use, and thus has to set them for each and every
> expression just in case.

I was wondering what the justification for $& affecting every regexp was.  
And now I know :)

But if you can't determine where $& is going to come from, why can you 
determine where $1 is going to come from (given that $& is essentially 
$0), such that you don't need to slow down all other regexps too?

-- 
Tim Connors



More information about the Melbourne-pm mailing list