http://www.perlmonks.org?node_id=820991

linuxer has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm currently working on a translation of perlvar (into German) and wonder, if I found a bug, or not.

The paragraph for %+ (%LAST_PAREN_MATCH) starts with:

Similar to "@+", the "%+" hash allows access to the named buffers, should they exist, in the last successful match in the currently active dynamic scope.

Now I wonder, how %+ is similar to @+ (which is @LAST_MATCH_END and described in the paragraph directly before %+).

@+ is about "offsets of the ends of the last successful submatches" and %+ about accessing "named capture buffers".

How can these two things be similar? Maybe I misinterpret the "similar to" (I am not a native English speaker).

I think, if %+ is similar to another variable, then it should be $+ ($LAST_PAREN_MATCH).

I checked the perldoc of 5.10.0 and 5.10.1. Both contain this bug (The feature of named capture buffers was introduced with 5.10.0).

And now let me ask for your wisdom:

Do you also consider that a bug, or not? If I am wrong, please help me to understand that part.

Thanking you in anticipation.

Replies are listed 'Best First'.
Re: Bug in ' perldoc perlvar ' ?
by Fletch (Bishop) on Feb 02, 2010 at 18:48 UTC

    The @+ and @- variables allow you to reference capture buffers by index rather by named variables (e.g. substr($var, $-[2], $+[2] - $-[2]) rather than $2). Similarly %+ allows access to named captures by "index" (the name).

    $ perl5.10.0 -E '$_="ab cd ef gh";m/(?<foo>\S+)\s+\S+\s+(?<bar>\S+)/; +for(keys %+){say "$_:\t$+{$_}"}; say "\$+: $+";' bar: ef foo: ab $+: ef

    It's not like $+ which is a special name for "whatever the last group was which matched"; $+ always going to refer to whatever the last group was which successfully matched, whereas %+ allows access to all of the named captures.

    That clear things up?

    Update: Oop, had @+ twice first sentence; thanks linuxer.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      Yes, that cleared it up. Thanks for that.

      Ok, though I understand that now, I think, that "similar to @+" should be rephrased to be more concrete what is meant (or may it even be left out? Is it really necessary?).

      added:

      If I read perlvar from the beginning to end, as we can see, I don't recognize this "similarity" of @+ and %+. @- is explained a few paragraphes later in the document, so hasn't been read yet. I don't think, it's clear enough (in the doc).

        I understand what it's saying, but I also can understand why it's not readily apparent (especially to a non-native speaker) because I'm having a hard time coming up with a clearer phrasing for it myself :). Maybe "Analogous to @+ for accessing captures via numeric indexes, %+ allows accessing named captures via the name"? If you want to access captures by their index, use @+ and @-; if you want to access (named) captures by name, use %+.

        The cake is a lie.
        The cake is a lie.
        The cake is a lie.

Re: Bug in ' perldoc perlvar ' ?
by ikegami (Patriarch) on Feb 02, 2010 at 20:04 UTC
    perlvar says

    Similar to @+, the %+ hash allows access to the named capture buffers

    @+ provides access to all capture buffers (not just the named ones), so the statement is unclear if not incorrect.

    @+ is about "offsets of the ends of the last successful submatches" and %+ about accessing "named capture buffers". How can these two things be similar?

    perlvar only claimed that one aspect was similar. It never said the kind of access to the capture buffers was similar. That one variable provides the offset of the end of the buffer while the other provides the contents of the buffer does not disprove the claim of similarity that perlvar made.