使用perl的`system` [英] Using perl's `system`
问题描述
我想使用perl的system()
运行一些命令(例如command
).假设command
是这样从shell运行的:
I would like to run some command (e.g. command
) using perl's system()
. Suppose command
is run from the shell like this:
command --arg1=arg1 --arg2=arg2 -arg3 -arg4
如何使用system()
通过这些参数运行command
?
How do I use system()
to run command
with these arguments?
推荐答案
最佳实践:避免使用shell,使用自动错误处理- IPC::System::Simple
.
Best practices: avoid the shell, use automatic error handling - IPC::System::Simple
.
require IPC::System::Simple;
use autodie qw(:all);
system qw(command --arg1=arg1 --arg2=arg2 -arg3 -arg4);
use IPC::System::Simple qw(runx);
runx [0], qw(command --arg1=arg1 --arg2=arg2 -arg3 -arg4);
# ↑ list of allowed EXIT_VALs, see documentation
之后会出现咆哮声.
eugene y的答案包括指向系统文档的链接.在那里,我们可以看到每次都需要包含一段宏大的代码才能正确执行system
.尤金(Eugene y)的答案只是其中的一部分.
eugene y's answer includes a link to the documentation to system. There we can see a homungous piece of code that needs to be included everytime to do system
properly. eugene y's answer shows but a part of it.
无论何时遇到这种情况,我们都会将重复的代码捆绑在一个模块中.我使用Try::Tiny
进行适当的简洁处理异常,但是IPC::System::Simple
正确完成了 system
,但并没有很快被社区接受.看来它需要更频繁地重复.
Whenever we are in such a situation, we bundle up the repeated code in a module. I draw parallels to proper no-frills exception handling with Try::Tiny
, however IPC::System::Simple
as system
done right did not see this quick adoption from the community. It seems it needs to be repeated more often.
因此,使用autodie
!使用IPC::System::Simple
!.确保自己使用乏味,并确保使用经过测试的代码.
So, use autodie
! Use IPC::System::Simple
! Save yourself the tedium, be assured that you use tested code.
这篇关于使用perl的`system`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!