Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: Bug in ' perldoc perlvar ' ?

by Fletch (Chancellor)
on Feb 02, 2010 at 18:48 UTC ( #821002=note: print w/replies, xml ) Need Help??

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

    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?).


    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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://821002]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2017-10-18 16:53 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (249 votes). Check out past polls.