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

While im pretty sure you know this, ill mention it for other readers:

@- and @+ are actually thin wrappers around the way that Perl internally tracks where matches and capture buffers start and stop. The variables $& and $1 and friends actually are ties that combine the data in @- and @+ with a copy of the string matched against to produce the magic variables. If you have ever wondered why @- and @+ exist and why they are such strange things its because they are really just tied variables that wrap C arrays*. Larry is on record saying that he considers them a bad interface because of this.

* in Perl 5.9.4 Nicholas Clark merged the two arrays into a single array of structs containing two STRLEN's (long integers).

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

Replies are listed 'Best First'.
Re^3: @- and @+ question
by Juerd (Abbot) on Jun 19, 2007 at 21:22 UTC

    Actually, I had no idea. Thanks for the lecture :)

      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