in reply to Bug in ' perldoc perlvar ' ?
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Bug in ' perldoc perlvar ' ?
by linuxer (Curate) on Feb 02, 2010 at 19:31 UTC | |
by Fletch (Bishop) on Feb 02, 2010 at 19:49 UTC |