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

Jacinta Richardson jarich at perltraining.com.au
Sun Jan 20 21:59:37 PST 2008


Tim Connors wrote:
> On Sat, 19 Jan 2008, Scott Penrose wrote:
> 
>> And for those who don't know.
>>
>> $& $' or $` anywhere in the code found by the compiler makes all  
>> regular expressions slow, not just the ones where it is used.
>>
>> (sorry about the separate emails)
> 
> 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.

	Jacinta

-- 
   ("`-''-/").___..--''"`-._          |  Jacinta Richardson         |
    `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
    (_Y_.)'  ._   )  `._ `. ``-..-'   |      +61 3 9354 6001        |
  _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
 (il),-''  (li),'  ((!.-'             |   www.perltraining.com.au   |


More information about the Melbourne-pm mailing list