clt60
clt60

Reputation: 63972

What is the meaning of "keys %+"?

What does this code mean?

keys %+

I saw it in some source code, and I don't know what Perl documentation to read to find out what it means.

Upvotes: 3

Views: 202

Answers (3)

Zaid
Zaid

Reputation: 37146

Whenever you see a groovy-looking variable, look it up in perldoc perlvar.

In this case, %+ is the hash that stores the values corresponding to the named captures of the last regex:

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. For example, $+{foo} is equivalent to $1 after the following match:

'foo' =~ /(?<foo>foo)/;

See perldoc perlretut for more details.

Upvotes: 5

toolic
toolic

Reputation: 62227

These docs are what you want:

perldoc -f keys

perldoc -v %+

Upvotes: 3

simbabque
simbabque

Reputation: 54371

The hash %+ has all the matches from named capture groups in regexes in the current scope. It's explained in perlvar under %LAST_PAREN_MATCH and was added in 5.10.

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. [..]

The keys function lists all the keys in the hash.

Upvotes: 4

Related Questions