TedYoung has asked for the wisdom of the Perl Monks concerning the following question:
Anyone who does anything significant with regular expressions learns that the variables $&, $' and $` imposes significant performance penalty on all regular expression matches. I have personally witnessed just how significant the use of these variables can be on performance (KinoSearch & Large Documents).
perlvar says under @- the following:
$` is the same as substr($var, 0, $-[0]) $& is the same as substr($var, $-[0], $+[0] - $-[0]) $' is the same as substr($var, $+[0])
So my question is why can't these variables be implemented this way behind the scenes. If I could get access to the last variable successfully matched against, for instance, I could write a Tie interface for these variables very easy.
I am guessing it has to do with keeping track of the last variable matched and handling cases where that variable is changed before reading $& (such as assignment or a s///).
This question is really out of curiosity about the internals of the perl regexp engine.
Thanks,
Ted Young
($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)