Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Perl: porters

Documentation bug or brain bug? (or, what \k<name> and $+{name} refer to)

 

 

Perl porters RSS feed   Index | Next | Previous | View Threaded


maddingue at free

May 5, 2008, 1:21 PM

Post #1 of 7 (152 views)
Permalink
Documentation bug or brain bug? (or, what \k<name> and $+{name} refer to)

Hello,


While writing an article (in French) about 5.10 regexps, a reviewer
found that what I wrote about \k<name> and $+{name} was inconsistent.
I reread perlre and perlvar, and it seems that the inconsistency lies
in perlvar:

perlre/"Capture buffers" says:

Outside the
pattern, a named
capture buffer is available via the "%+" hash. When
different buffers
within the same pattern have the same name, $+{name} and
"\k<name>"
refer to the leftmost defined group.

and later in the "Extended Patterns" section:

(?<NAME>pattern)
[...]
If multiple distinct capture buffers have the same
name then
the $+{NAME} will refer to the leftmost defined
buffer in the
match.
[...]

\k<NAME>
Named backreference. Similar to numeric
backreferences,
except that the group is designated by name and not
number.
If multiple groups have the same name then it
refers to the
leftmost defined group in the current match.

But perlvar says:

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


Now, if I understand correctly, perlre says that \k and %+ refers to
the leftmost, IOW the *first* successful match, while perlvar says
that %+ refers to the *last* successful match.
Or am I misunderstanding something?


--
Sébastien Aperghis-Tramoni

Close the world, txEn eht nepO.


davidnicol at gmail

May 5, 2008, 1:52 PM

Post #2 of 7 (145 views)
Permalink
Re: Documentation bug or brain bug? (or, what \k<name> and $+{name} refer to) [In reply to]

On Mon, May 5, 2008 at 3:21 PM, Sébastien Aperghis-Tramoni
<maddingue[at]free.fr> wrote:
>
> Now, if I understand correctly, perlre says that \k and %+ refers to the
> leftmost, IOW the *first* successful match, while perlvar says that %+
> refers to the *last* successful match.
> Or am I misunderstanding something?

Names, freed of the restriction of ordinality, may repeat, and
repetition of names is supported.

Last successful match refers to most recent regexp that matched; first
successful match refers to the leftmost of multiple conominal capture
groups within the last successful match.

Si cela peut être fait à clairifiant en français je considérerai
apprendre le français.


("conominal" is supposed to mean "with the same name")


rgarciasuarez at gmail

May 6, 2008, 1:48 AM

Post #3 of 7 (139 views)
Permalink
Re: Documentation bug or brain bug? (or, what \k<name> and $+{name} refer to) [In reply to]

2008/5/5 Sébastien Aperghis-Tramoni <maddingue[at]free.fr>:
> While writing an article (in French) about 5.10 regexps, a reviewer found
> that what I wrote about \k<name> and $+{name} was inconsistent. I reread
> perlre and perlvar, and it seems that the inconsistency lies in perlvar:
>
> perlre/"Capture buffers" says:
>
> Outside the pattern, a
> named
> capture buffer is available via the "%+" hash. When different
> buffers
> within the same pattern have the same name, $+{name} and "\k<name>"
> refer to the leftmost defined group.
>
> and later in the "Extended Patterns" section:
>
> (?<NAME>pattern)
> [...]
> If multiple distinct capture buffers have the same name
> then
> the $+{NAME} will refer to the leftmost defined buffer in
> the
> match.
> [...]
>
> \k<NAME>
> Named backreference. Similar to numeric backreferences,
> except that the group is designated by name and not number.
> If multiple groups have the same name then it refers to the
> leftmost defined group in the current match.
>
> But perlvar says:
>
> %+ Similar to @+, the %+ hash allows access to the named
> capture buffers, should they exist, in the last successful
> match in the currently active dynamic scope.
>
>
> Now, if I understand correctly, perlre says that \k and %+ refers to the
> leftmost, IOW the *first* successful match, while perlvar says that %+
> refers to the *last* successful match.
> Or am I misunderstanding something?

I think you are. The last successful match is the last =~ test that
matched successfully; the currently active dynamic scope is the
current pair of {} braces. We're not inside a regexp there.

If you find a better wording I'll be glad to apply.


maddingue at free

May 6, 2008, 3:01 AM

Post #4 of 7 (139 views)
Permalink
Re: Documentation bug or brain bug? (or, what \k<name> and $+{name} refer to) [In reply to]

Rafael Garcia-Suarez wrote:

> 2008/5/5 Sébastien Aperghis-Tramoni <maddingue[at]free.fr>:
>
> > Now, if I understand correctly, perlre says that \k and %+ refers to the
> > leftmost, IOW the *first* successful match, while perlvar says that %+
> > refers to the *last* successful match.
> > Or am I misunderstanding something?
>
> I think you are. The last successful match is the last =~ test that
> matched successfully; the currently active dynamic scope is the
> current pair of {} braces. We're not inside a regexp there.

So $+{name} is the first successful capture from the last regexp with
a group of that name? (I think I know how it works, but I'm trying to
find the way to correctly express it.)


--
Sébastien Aperghis-Tramoni

Close the world, txEn eht nepO.


rgarciasuarez at gmail

May 6, 2008, 3:10 AM

Post #5 of 7 (139 views)
Permalink
Re: Documentation bug or brain bug? (or, what \k<name> and $+{name} refer to) [In reply to]

2008/5/6 Sébastien Aperghis-Tramoni <maddingue[at]free.fr>:
> So $+{name} is the first successful capture from the last regexp with
> a group of that name? (I think I know how it works, but I'm trying to
> find the way to correctly express it.)

No, it's the 1st successful capture named "name" in the last
successful regexp in scope. If that regexp didn't had any "name"
capture, that doesn't matter. Just like $17 is the 17th capture in the
last successful regexp in scope, even if it had only 16 pairs of
parentheses.

perl -wE '"foo"=~/(.)(.)/;"bar"=~/(.)/;say $1,$2'
Use of uninitialized value $2 in say at -e line 1.
b


pagaltzis at gmx

May 6, 2008, 10:22 AM

Post #6 of 7 (137 views)
Permalink
Re: Documentation bug or brain bug? (or, what \k<name> and $+{name} refer to) [In reply to]

* Rafael Garcia-Suarez <rgarciasuarez[at]gmail.com> [2008-05-06 12:15]:
> 2008/5/6 Sébastien Aperghis-Tramoni <maddingue[at]free.fr>:
>> So $+{name} is the first successful capture from the last
>> regexp with a group of that name? (I think I know how it
>> works, but I'm trying to find the way to correctly express
>> it.)
>
> No, it's the 1st successful capture named "name" in the last
> successful regexp in scope. If that regexp didn't had any
> "name" capture, that doesn't matter. Just like $17 is the 17th
> capture in the last successful regexp in scope, even if it had
> only 16 pairs of parentheses.
>
> perl -wE '"foo"=~/(.)(.)/;"bar"=~/(.)/;say $1,$2'
> Use of uninitialized value $2 in say at -e line 1.
> b

To summarise:

=over 4

=item *

If a regex has multiple named captures with the same name, then
the B<left-most matched> capture will be used.

=item *

If a regex B<completely fails> to match (ie. returns false), then
the %+ hash is not cleared.

=item *

If a regex succeeds, then the %+ hash B<is cleared> and all
previously stored named captures are lost; it will contain
key/value pairs B<only for named captures of this last regex>.

=back

Regards,
--
Aristotle Pagaltzis // <http://plasmasturm.org/>


demerphq at gmail

May 12, 2008, 5:54 AM

Post #7 of 7 (108 views)
Permalink
Re: Documentation bug or brain bug? (or, what \k<name> and $+{name} refer to) [In reply to]

2008/5/6 Aristotle Pagaltzis <pagaltzis[at]gmx.de>:
> * Rafael Garcia-Suarez <rgarciasuarez[at]gmail.com> [2008-05-06 12:15]:
>
>
> > 2008/5/6 Sébastien Aperghis-Tramoni <maddingue[at]free.fr>:
> >> So $+{name} is the first successful capture from the last
> >> regexp with a group of that name? (I think I know how it
> >> works, but I'm trying to find the way to correctly express
> >> it.)
> >
> > No, it's the 1st successful capture named "name" in the last
> > successful regexp in scope. If that regexp didn't had any
> > "name" capture, that doesn't matter. Just like $17 is the 17th
> > capture in the last successful regexp in scope, even if it had
> > only 16 pairs of parentheses.
> >
> > perl -wE '"foo"=~/(.)(.)/;"bar"=~/(.)/;say $1,$2'
> > Use of uninitialized value $2 in say at -e line 1.
> > b
>
> To summarise:
>
> =over 4
>
> =item *
>
> If a regex has multiple named captures with the same name, then
> the B<left-most matched> capture will be used.
>
> =item *
>
> If a regex B<completely fails> to match (ie. returns false), then
> the %+ hash is not cleared.
>
> =item *
>
> If a regex succeeds, then the %+ hash B<is cleared> and all
> previously stored named captures are lost; it will contain
> key/value pairs B<only for named captures of this last regex>.
>
> =back

Good description.


--
perl -Mre=debug -e "/just|another|perl|hacker/"

Perl porters RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.