Question

I was wondering if there was an operator name for %+, so instead of code like:

/(?<CaptureName>\w+)/;
...
my $whatever=$+{CaptureName};

I could use something more readable:

use English;

/(?<CaptureName>\w+)/;
...
my $whatever=$?????{CaptureName};
Was it helpful?

Solution

Using the English module you can refer to it as %LAST_PAREN_MATCH:

use English;

/(?<CaptureName>\w+)/;
...
my $whatever = $LAST_PAREN_MATCH{CaptureName};

OTHER TIPS

perldoc -v %+

   %LAST_PAREN_MATCH
   %+      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.

You can refer to http://perldoc.perl.org/perlvar.html om he future for finding out the symbol names.

In your case the sylbe is called LAST_PAREN_MATCH

%LAST_PAREN_MATCH
%+
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

The only note I'd make is that the docs includes this:

This variable was added in Perl v5.10.0.

So if you're using an older interpreter this could cause problems.

NOTE as Keith points out int he comment below, you can also use perldoc -v '$+'. This has the benefit of only working if the symbol is available on your installed version of Perl.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top