为什么`使用5.005;`有副作用?

时间:2014-02-04 08:18:21

标签: perl

此代码段的行为会有所不同,具体取决于是否存在use 5.005;(或任何其他版本)。

为什么?

perldoc -f use我没有看到任何可疑的东西。

#!/usr/bin/perl -w
use strict;

# If this is present, "Exiting\n" is printed, but the process doesn't exit.
# If commented out, exit terminates this process.
# Go figure
use 5.005;

# Open a sub-process that is "long-lived".
open FH, 'perl -e "sleep 600" |'
    or die $!;

$SIG{ALRM} = sub {
    print "Exiting\n";
    exit;
};
alarm(1);

<FH>;

在ubuntu 12.04 perl版本5.14.2和debian squeeze perl版本5.10.1上测试

P.S。:我不是在寻找解决方法,而是在解释。

1 个答案:

答案 0 :(得分:2)

我无法在Fedora 20上使用Perl 5.18重现这种行为。所以这是在黑暗中拍摄的。

我相信当你最终等待子进程时,它会由Perl自动引起closing the filehandle from a piped open(“关闭任何管道文件句柄会导致父进程等待子进程完成,然后返回状态值$?${^CHILD_ERROR_NATIVE})。

“在黑暗中拍摄”是因为我怀疑某些版本的Perl没有自动关闭管道打开的文件句柄。而且你已经设法得到了这种行为,这意味着你的过程不必等待孩子退出。不幸的是,我无法找到任何官方的相关信息。

相关问题