Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^4: @- and @+ question

by demerphq (Chancellor)
on Jun 19, 2007 at 22:35 UTC ( [id://622132]=note: print w/replies, xml ) Need Help??


in reply to Re^3: @- and @+ question
in thread @- and @+ question

Oh, :-), well then Ill add another point. Mostly everybody knows that $` and $& and $' incur a global performance penalty. What they probably dont know is why, and the reason is the part where I mentioned that the magic regex vars work against a copy of the original string (not always actually, but pretty close). In the case of capture buffers the regex engine knows it needs to copy the string because it knows the pattern has capture buffers in it. However for the evil regex vars there is no such cue in the pattern and when combined with the dynamic scoping of these vars Perl has to make a serious pessimisation to ensure that they always work: once it sees any of them it has to copy the original string for every match.

This is why in perl 5.10 we have the /p modifier. It tells Perl that we want to use $` $& and $' on the results of the pattern match. Except that we cant change the behaviour of $& and friends so it actually tells Perl that we want to use their non-evil counterparts ${^PREMATCH}, ${^MATCH} or ${^POSTMATCH} on the result of the match. Since the /p tells perl "copy the string when you do the match" there doesnt need to be a global effect, ${^MATCH} and friends only work when the pattern is executed with the /p modifier, and the programmer knows this. So perl doesnt have to be "clever" and pessimise to ensure that they will always work once its seen them.

---
$world=~s/war/peace/g

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://622132]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-25 13:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found