If you’re writing to a pipe,you should also trap SIGPIPE. Otherwise,
think of what happens when you start up a pipe to a command that
doesn’t exist: the open() will in all likelihood succeed (it only
reflects the fork()’s success),but then your output will
fail–spectacularly. Perl can’t know whether the command worked
because your command is actually running in a separate process whose
exec() might have Failed. Therefore,while readers of bogus commands
return just a quick end of file,writers to bogus command will trigger
a signal they’d better be prepared to handle. Consider:
open(FH,"|bogus") or die "can't fork: $!"; print FH "bang\n" or die "can't write: $!"; close FH or die "can't close: $!";
That won’t blow up until the close,and it will blow up with a
SIGPIPE. To catch it,you could use this:
$SIG{PIPE} = 'IGNORE'; open(FH,"|bogus") or die "can't fork: $!"; print FH "bang\n" or die "can't write: $!"; close FH or die "can't close: status=$?";
如果我正确地阅读它,它说第一个版本可能不会在最后关闭之前死掉.
但是,我的OS X盒子上没有发生这种情况(Perl版本5.8.9到5.15.9).无论我是否在那里有$SIG {PIPE}行,它都会打开“无法分叉:没有这样的文件或目录”.
我有什么误会?
解决方法
https://github.com/mirrors/perl/commit/d5a9bfb0fc8643b1208bad4f15e3c88ef46b4160
它也在http://search.cpan.org/dist/perl/pod/perlopentut.pod#Pipe_Opens中记录
它本身指向perlipc,但perlipc确实似乎缺少这个