使用IO::Pipe Perl的管道STDERR


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

如果some_function产生一些异常或STDERR,会发生什么?是否写入$writer?如果没有,如何才能做到这一点?

您需要捕获$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

最新更新