perl - 使用 perl 的 `system`

标签 perl system execution

我想使用 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::Simplesystem做得对并没有看到社区迅速采用。似乎需要更频繁地重复。

所以,使用 autodie !使用IPC::System::Simple ! 省去自己的乏味,请确保您使用经过测试的代码。

关于perl - 使用 perl 的 `system`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3477916/

相关文章:

perl - 在 Perl 中声明变量

perl - 我如何使用 Perl 的 Net::Pcap::Reassemble 重组 IP 片段?

c++ - 为什么 system() 失败并显示错误代码 127?

javascript - 谷歌反向地理编码返回变量

testing - 如果其中一个测试在 NUnit 中失败,如何停止在 TestFixture 中进一步执行测试?

perl - CGI中使用POST方法提交表单时GET参数丢失?

perl - 类型::微小而深刻的强制转换

html - 信誉系统 : weighted points vs unweighted points?

java - 如何在 Java 中设置 Windows 通知图标出现的时间长度

c# - 并行和异步方法