in reply to @- and @+ question

The values of the overall match, (index 0), should be the same as the values for the one captured group, (index 1),

Only if the first paren group is around the entire regex, which it isn't. Index 0 describes $&, 1 does $1, 2 does $2, etcetera, only without actually using those globals.

Juerd # { site => 'juerd.nl', do_not_use => 'spamtrap', perl6_server => 'feather' }

Replies are listed 'Best First'.
Re^2: @- and @+ question
by demerphq (Chancellor) on Jun 19, 2007 at 19:55 UTC

    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

      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

Re^2: @- and @+ question
by Cristoforo (Curate) on Jun 19, 2007 at 19:33 UTC
    Index 0 describes $&,

    Thanks for the clarification - I read that in the docs but I didn't see it right.