是否有特殊变量%+的名称?

Mic*_*eyn 6 perl perlop

我想知道是否有一个运营商名称%+,所以代替以下代码:

/(?<CaptureName>\w+)/;
...
my $whatever=$+{CaptureName};
Run Code Online (Sandbox Code Playgroud)

我可以使用更具可读性的东西:

use English;

/(?<CaptureName>\w+)/;
...
my $whatever=$?????{CaptureName};
Run Code Online (Sandbox Code Playgroud)

Rob*_*arl 7

使用英语模块,您可以将其称为%LAST_PAREN_MATCH:

use English;

/(?<CaptureName>\w+)/;
...
my $whatever = $LAST_PAREN_MATCH{CaptureName};
Run Code Online (Sandbox Code Playgroud)


too*_*lic 6

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.
Run Code Online (Sandbox Code Playgroud)


cho*_*ida 6

你可以参考http://perldoc.perl.org/perlvar.html以后找到符号名称.

在您的情况下,sylbe称为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.
Run Code Online (Sandbox Code Playgroud)

例如,$ + {foo}在以下之后相当于$ 1

我要做的唯一一点就是文档包括:

This variable was added in Perl v5.10.0.
Run Code Online (Sandbox Code Playgroud)

因此,如果您使用较旧的翻译,这可能会导致问题.

请注意,正如Keith在下面的评论中指出的那样,您也可以使用perldoc -v '$+'.只有符号在您安装的Perl版本上可用时才有效.

  • 或者运行`perldoc perlvar`,或'perldoc -v'$ +'`.运行`perldoc`命令的优势在于它将向您显示系统中Perl版本的文档,而不是网页上记录的文档. (3认同)