使用 perl 的 `system` [英] Using perl's `system`

查看:30
本文介绍了使用 perl 的 `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 文档的链接.在那里,我们可以看到每次都需要包含一段类似的代码才能正确执行 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屋!

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