我想知道每次Perl反引号(引用)运算符返回非零错误代码而不是每次调用后检查$? >> 8
时是否可以捕获(调用子例程)。
有可能吗?
答案 0 :(得分:4)
IPC::System::Simple的capture
是一个反引号版本,会在出错时抛出异常。
答案 1 :(得分:2)
你可以将反引号包装在一个实现错误检查的函数中,然后调用它。那将是你自己实施的挂钩系统。
sub call_backticks {
my ($command, $callback) = @_;
my $output = `$command`;
return $callback->($command, $?) if $? >> 8;
return $output;
}
# later...
my $output = call_backticks('cat /var/log/messages', sub { print Dumper @_; });
这将为您提供基本的处理。但是在Perl中并没有普遍的联系。