gpt4 book ai didi

perl - 在作用域为 `no autodie` 之后,程序在 `*STDOUT` 处终止

转载 作者:行者123 更新时间:2023-12-03 23:43:55 26 4
gpt4 key购买 nike

这个节目

use warnings;
use strict;
use feature qw(say);

{
#use autodie; # all good when this is uncommented
no autodie;
}

open my $OLDSTD, '>&', *STDOUT; #--> line 10 (program fails)
open *STDOUT, '>', 'stdout.out';

say "$$ done";
中止
Undefined subroutine called at problem_no_autodie.pl line 10.

To restate the comment in code: if there is a use autodie; statement first then all is well. (All is well with only use autodie; as well.) Curiously, in the same scope with no autodie statement I see no such problems either; only code outside of its scope fails! Kinda anti-scoping, eh?

If this scoped no autodie comes after the use of *STDOUT then it's all good again. Further use of *STDOUT, after (the scoped) no autodie, fails the program.

There is a Gotcha mentioned in docs that involves barewords (which I don't fully understand), and the program indeed fails with STDOUT -- but I have it as *STDOUT.

So it appears that *STDOUT is treated as a user's sub, but I don't understand that nor how the scope of autodie gets defeated. (Scope leakage is mentioned as a bug in some versions but in a seemingly unrelated way.) And there is a practical problem with this.

I don't use autodie in my code. But consider this sub, which I do use

sub isatty {
no autodie;
state $isatty = open(my $tty, '+<', '/dev/tty');
return $isatty;
}
这是合法的 open失败,所以我们必须禁用 autodie在那个范围内,以防 sub 的用户打开它。那么所描述的行为会受到伤害吗?在什么情况下?
我对 no autodie的这种效果感到困惑以及它的泄漏超出其范围,以及所有奇怪的细节。但真正令人担忧的是,我不确定如何保护使用上述库的代码免受该行为的影响,因为我不理解它。有任何想法吗?
我在 CentOS 7.8 上的 5.16.3(系统)、5.26.2 和 5.30.0(perlbrew)下看到了这个
我在 5.32.0 上没有看到这种行为;没有失败。
... or die $!查看 open没有任何区别,因此为简单起见没有显示。

最佳答案

This bug在 autodie 2.24 中引入并在 autodie 2.30 中修复。

$ ( cd ./autodie-2.23; perl Makefile.PL >&3; make >&3; perl -Mblib ../a.pl && echo ok ) 3>/dev/null
ok

$ ( cd ./autodie-2.24; perl Makefile.PL >&3; make >&3; perl -Mblib ../a.pl && echo ok ) 3>/dev/null
Undefined subroutine called at ../a.pl line 10.

$ ( cd ./autodie-2.29; perl Makefile.PL >&3; make >&3; perl -Mblib ../a.pl && echo ok ) 3>/dev/null
Undefined subroutine called at ../a.pl line 10.

$ ( cd ./autodie-2.30; perl Makefile.PL >&3; make >&3; perl -Mblib ../a.pl && echo ok ) 3>/dev/null
ok

$ ( cd ./autodie-2.31; perl Makefile.PL >&3; make >&3; perl -Mblib ../a.pl && echo ok ) 3>/dev/null
ok
(在 CPAN 上找不到 2.30。)
为防止出现此问题,请添加对 autodie 2.30 或更高版本的依赖。

关于perl - 在作用域为 `no autodie` 之后,程序在 `*STDOUT` 处终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64096831/

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