gpt4 book ai didi

perl - perl v24 中的 ${^MATCH} 和/p 修饰符

转载 作者:行者123 更新时间:2023-12-04 14:55:27 25 4
gpt4 key购买 nike

perlre说:

p

Preserve the string matched such that ${^PREMATCH} , ${^MATCH} , and ${^POSTMATCH} are available for use after matching.

In Perl 5.20 and higher this is ignored. Due to a new copy-on-write mechanism, ${^PREMATCH} , ${^MATCH} , and ${^POSTMATCH} will be available after the match regardless of the modifier.



还有 perlvar

${^MATCH} This is similar to $& ($MATCH ) except that it does not incur the performance penalty associated with that variable.

In Perl v5.18 and earlier, it is only guaranteed to return a defined value when the pattern was compiled or executed with the /p modifier. In Perl v5.20, the /p modifier does nothing, so ${^MATCH} does the same thing as $MATCH .



测试:
echo 'Lorem ipsum dolor sit ut dicta qui dolores.' |\
perl -nE 'say ${^MATCH} while m/dolor/g'

输出:两个空行(2x \n)

但是:
echo 'Lorem ipsum dolor sit ut dicta qui dolores.' |\
perl -nE 'say ${^MATCH} while m/dolor/gp'

输出:
dolor
dolor

我的perl:
$ perl -v

This is perl 5, version 24, subversion 0 (v5.24.0) built for darwin-2level
(with 1 registered patch, see perl -V for more detail)

所以在 5.24.0 /p还需要修改器吗?或者我错过了什么?

最佳答案

代码的行为与记录在案的行为之间确实存在差异。提交错误报告作为票 RT#131087 .

作为解决方法,您可以使用 $& ( $MATCH )。

关于perl - perl v24 中的 ${^MATCH} 和/p 修饰符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43159304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com