如何在 Perl 中同时捕获 stderr、stdout 和退出代码? [英] How do you capture stderr, stdout, and the exit code all at once, in Perl?

查看:29
本文介绍了如何在 Perl 中同时捕获 stderr、stdout 和退出代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从 Perl 运行外部进程,捕获其 stderr、stdout 和进程退出代码?

Is it possible to run an external process from Perl, capture its stderr, stdout AND the process exit code?

我似乎能够将这些组合起来,例如使用反引号获取标准输出,使用 IPC::Open3 获取输出,使用 system() 获取退出代码.

I seem to be able to do combinations of these, e.g. use backticks to get stdout, IPC::Open3 to capture outputs, and system() to get exit codes.

如何同时捕获 stderr、stdout 和退出代码?

How do you capture stderr, stdout, and the exit code all at once?

推荐答案

如果你重新阅读 IPC::Open3 的文档,你会看到一个注释,你应该调用 waitpid 来获取子进程.执行此操作后,状态应该在 $? 中可用.退出值为 $?>>8.看$? in perldoc perlvar.

If you reread the documentation for IPC::Open3, you'll see a note that you should call waitpid to reap the child process. Once you do this, the status should be available in $?. The exit value is $? >> 8. See $? in perldoc perlvar.

这篇关于如何在 Perl 中同时捕获 stderr、stdout 和退出代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆