6

我想知道是否有一个运算符名称%+,而不是像这样的代码:

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

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

use English;

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

3 回答 3

7

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

use English;

/(?<CaptureName>\w+)/;
...
my $whatever = $LAST_PAREN_MATCH{CaptureName};
于 2014-02-12T19:20:29.757 回答
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.
于 2014-02-12T19:21:41.463 回答
6

您可以参考http://perldoc.perl.org/perlvar.html om 他将来查找符号名称。

在你的情况下,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.

例如,$+{foo} 等价于 $1 在下面

我要做的唯一说明是文档包括以下内容:

This variable was added in Perl v5.10.0.

因此,如果您使用的是较旧的解释器,这可能会导致问题。

注意正如基思在下面的评论中指出的那样,您也可以使用perldoc -v '$+'. 这样做的好处是只有在您安装的 Perl 版本上可用该符号时才有效。

于 2014-02-12T19:22:51.230 回答