我想使用 perl 的 command
运行一些命令(例如 system()
) .假设 command
像这样从 shell 运行:
command --arg1=arg1 --arg2=arg2 -arg3 -arg4
如何使用
system()
运行 command
有这些论点?
最佳答案
最佳实践:避免使用 shell,使用自动错误处理 - 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's answer包括指向系统文档的链接。在那里我们可以看到每次执行
system
时都需要包含一段相似的代码。适本地。 eugene y 的回答显示了其中的一部分。每当我们遇到这种情况时,我们都会将重复的代码捆绑在一个模块中。我用
Try::Tiny
绘制了与适当的简洁异常处理的相似之处。 ,但是 IPC::System::Simple
如system
做得对并没有看到社区迅速采用。似乎需要更频繁地重复。所以,使用
autodie
!使用IPC::System::Simple
! 省去自己的乏味,请确保您使用经过测试的代码。
关于perl - 使用 perl 的 `system`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3477916/