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};
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 −
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.
__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.
__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.
Using the English module you can refer to it as %LAST_PAREN_MATCH
:
use English;
/(?<CaptureName>\w+)/;
...
my $whatever = $LAST_PAREN_MATCH{CaptureName};
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With