in reply to Re: Finally, a $& compromise!
in thread Finally, a $& compromise!
Does this mean that a regexp that captures $1 implies $& which implies the performance hit for maintaining $`, $&, and $' ?Only for maintaining them for that regex. The way that $DIGIT variables are supported is thus:
The $DIGIT vars are like tiny instances of $& that only appear when you need them. $& appears all the time if you use it once. Here's an example that shows that a regex that uses capturing parentheses gives you the ability to use $& and the like. These are three separate programs. I'm using eval '' so that $& isn't seen at the time the regexes are executed.
Does that make sense? In order to have $1, you have to have the string that is also used for $&. From perlre:#!/usr/bin/perl "simple" =~ /im/ and eval q{ print "<$`><$&><$'>\n" }; ### #!/usr/bin/perl "complex" =~ /.p/ and eval q{ print "<$`><$&><$'>\n" }; #<co><mp><lex> ### #!/usr/bin/perl "capture" =~ /(.t)./ and eval q{ print "<$`><$&><$'>:<$1>\n" }; #<ca><ptu><re>:<pt>
WARNING: Once Perl sees that you need one of $&, $`, or $' anywhere in the program, it has to provide them for every pattern match. This may substantially slow your program. Perl uses the same mechanism to produce $1, $2, etc, so you also pay a price for each pattern that contains capturing parentheses. (To avoid this cost while retaining the grouping behaviour, use the extended regular expression (?: ... ) instead.) But if you never use $&, $` or $', then patterns without capturing parentheses will not be penalized. So avoid $&, $', and $` if you can, but if you can't (and some algorithms really appreciate them), once you've used them once, use them at will, because you've already paid the price. As of 5.005, $& is not so costly as the other two.Some of that will be rewritten with the advent of this pragma, though. It's nice to "rewrite the books".
_____________________________________________________
Jeff[japhy]Pinyan:
Perl,
regex,
and perl
hacker.
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;
|
---|