使用IO :: Pipe Perl管道STDERR

时间:2018-10-29 08:56:40

标签: perl pipe

my $writer = IO::Pipe->new();
$writer->writer();
print $writer $some_function();
$writer->flush();
$writer->close;

如果some_function产生某些异常或STDERR,会发生什么?是否写入$writer?如果没有,怎么能做到?

1 个答案:

答案 0 :(得分:0)

您需要抓住$SIG{__WARN__}和/或$SIG{__DIE__}

{
    my $writer = IO::Pipe->new();
    $writer->writer();
    local $SIG{__WARN__} = sub { print $writer $_[0] };
    local $SIG{__DIE__} = sub { print $writer $_[0] };
    print $writer $some_function();
    $writer->flush();
    $writer->close;
}

$SIG{__DIE__}处理程序需要进行修改,具体取决于您要如何处理严重错误。

https://perldoc.perl.org/perlvar.html#%25SIG