gpt4 book ai didi

perl - Perl 5.8 与 5.10 和 5.12 的子例程中的代码延迟评估有什么区别?

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

这段代码在 Perl 5.8 下的行为与在 Perl 5.12 下的行为不同:

my $badcode = sub { 1 / 0 };
print "Made it past the bad code.\n";

当我在 5.8 下运行它时,即使我从未执行除法,我也会收到错误消息:
[brock@chase tmp]$ /usr/bin/perl -v  This is perl, v5.8.8 built for i486-linux-gnu-thread-multi[brock@chase tmp]$ /usr/bin/perl badcode.pl  Illegal division by zero at badcode.pl line 1.[brock@chase tmp]$ /usr/local/bin/perl -v  This is perl 5, version 12, subversion 0 (v5.12.0) built for i686-linux  [brock@chase tmp]$ /usr/local/bin/perl badcode.pl  Made it past the bad code.

Under perl 5.10.1, it behaves as it does under 5.12:

brock@laptop:/var/tmp$ perl -vThis is perl, v5.10.1 (*) built for i486-linux-gnu-thread-multibrock@laptop:/var/tmp$ perl badcode.pl  Made it past the bad code.

I get the same results with a named subroutine, e.g.

sub badcode { 1 / 0 }

我在 perl5100delta pod 中没有看到任何关于此的内容。这是一个未记录的更改吗?其他一些变化的意外副作用? (为了记录,我认为 5.10 和 5.12 正在做正确的事情。)

最佳答案

我相信这是计划好的,我确实看到提到了 in perl5100delta.pod :

Exceptions in constant folding

The constant folding routine is now wrapped in an exception handler, and if folding throws an exception (such as attempting to evaluate 0/0), perl now retains the current optree, rather than aborting the whole program. Without this change, programs would not compile if they had expressions that happened to generate exceptions, even though those expressions were in code that could never be reached at runtime. (Nicholas Clark, Dave Mitchell)



它只是与除零异常有关,不会导致编译阶段中止。

关于perl - Perl 5.8 与 5.10 和 5.12 的子例程中的代码延迟评估有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2768167/

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