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. :-)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Can we make $& better?
by ikegami (Patriarch) on Sep 06, 2007 at 18:20 UTC | |
by Anno (Deacon) on Sep 06, 2007 at 19:40 UTC | |
Re: Can we make $& better? (no need with $^MATCH)
by grinder (Bishop) on Sep 06, 2007 at 19:45 UTC | |
by tye (Sage) on Sep 06, 2007 at 21:22 UTC | |
by bart (Canon) on Sep 07, 2007 at 20:28 UTC | |
by demerphq (Chancellor) on Sep 12, 2007 at 18:22 UTC | |
by tye (Sage) on Sep 12, 2007 at 19:12 UTC | |
by demerphq (Chancellor) on Sep 12, 2007 at 19:24 UTC | |
| |
Re: Can we make $& better?
by ferreira (Chaplain) on Sep 06, 2007 at 19:59 UTC | |
by demerphq (Chancellor) on Sep 12, 2007 at 18:07 UTC |