gpt4 book ai didi

regex - Perl 正则表达式/o 优化或错误?

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

我正在通读 perldoc perlre 并注意到这行有点滑稽:

o  - pretend to optimize your code, but actually introduce bugs

我搜索了文档的其余部分,但没有找到对提到的“错误”的另一种尊重。

有人知道使用/o 标志的错误是什么吗?

最佳答案

我在 perldoc perlfaq6 中找到了这个:

In versions 5.6 and later, Perl won't recompile the regular expressionif the variable hasn't changed, so you probably don't need the "/o"option. It doesn't hurt, but it doesn't help either. If you want anyversion of Perl to compile the regular expression only once even ifthe variable changes (thus, only using its initial value), you stillneed the "/o".

You can watch Perl's regular expression engine at work to verify foryourself if Perl is recompiling a regular expression. The "use re'debug'" pragma (comes with Perl 5.005 and later) shows the details.With Perls before 5.6, you should see "re" reporting that itscompiling the regular expression on each iteration. With Perl 5.6 orlater, you should only see "re" report that for the first iteration.

    use re 'debug';

my $regex = 'Perl';
foreach ( qw(Perl Java Ruby Python) ) {
print STDERR "-" x 73, "\n";
print STDERR "Trying $_...\n";
print STDERR "\t$_ is good!\n" if m/$regex/;
}

所以拥有/o 标志“没有坏处”。但“这也无济于事”。

在我看来这不完全是一个错误。

关于regex - Perl 正则表达式/o 优化或错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68454089/

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