Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a name for the special variable %+?

Tags:

perl

perlop

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};
like image 225
Michael Goldshteyn Avatar asked Feb 12 '14 19:02

Michael Goldshteyn


People also ask

What are special variables?

These variables are reserved for specific functions. For example, the $ character represents the process ID number, or PID, of the current shell − $echo $$ The above command writes the PID of the current shell − 29949. The following table shows a number of special variables that you can use in your shell scripts −

What is a name of a variable?

A variable is a symbolic name for (or reference to) information. The variable's name represents what information the variable contains. They are called variables because the represented information can change but the operations on the variable remain the same.

What is the use of special variable __ name __?

__name__ is a built-in variable which evaluates to the name of the current module. Thus it can be used to check whether the current script is being run on its own or being imported somewhere else by combining it with if statement, as shown below.

What are the special variables in Python?

__name__ (A Special variable) in Python Unlike other programming languages, python is not designed to start execution of the code from a main function explicitly. A special variable called __name__ provides the functionality of the main function.


3 Answers

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

use English;

/(?<CaptureName>\w+)/;
...
my $whatever = $LAST_PAREN_MATCH{CaptureName};
like image 118
RobEarl Avatar answered Oct 13 '22 02:10

RobEarl


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.
like image 40
toolic Avatar answered Oct 13 '22 01:10

toolic


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.

like image 31
chollida Avatar answered Oct 13 '22 02:10

chollida