Perl 中的 system、exec 和反引号之间有什么区别?

发布于 2024-08-03 18:52:58 字数 461 浏览 4 评论 0原文

在 Perl 中,从我的脚本运行另一个 Perl 脚本,或运行任何系统命令,例如 mvcppkgaddpkgrmpkginforpm 等,我们可以使用以下内容:

  • system()
  • exec()
  • ``(反引号)

这三个是相同的还是不同的?在每种情况下,这三者都给出相同的结果吗?它们在不同的场景中使用吗,比如调用 Perl 程序,我们必须使用 system(),而对于其他程序,我们必须使用 ``(反引号)。

请告知,因为我目前正在使用 system() 进行所有调用。

In Perl, to run another Perl script from my script, or to run any system commands like mv, cp, pkgadd, pkgrm, pkginfo, rpm etc, we can use the following:

  • system()
  • exec()
  • `` (Backticks)

Are all the three the same, or are they different? Do all the three give the same result in every case? Are they used in different scenarios, like to call a Perl program we have to use system() and for others we have to use ``(backticks).

Please advise, as I am currently using system() for all the calls.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

怪我入戏太深 2024-08-10 18:52:58

它们都是不同的,文档解释了它们的不同之处。反引号捕获并返回输出; system 返回退出状态,而 exec 如果成功则根本不会返回。

They're all different, and the docs explain how they're different. Backticks capture and return output; system returns an exit status, and exec never returns at all if it's successful.

无人问我粥可暖 2024-08-10 18:52:58

IPC::System::Simple 可能就是您想要的。

它为反引号、system() 和其他 IPC 命令提供了安全、可移植的替代方案。

它还允许您避免使用大多数上述命令的 shell,这在某些情况下会很有帮助。

IPC::System::Simple is probably what you want.

It provides safe, portable alternatives to backticks, system() and other IPC commands.

It also allows you to avoid the shell for most of said commands, which can be helpful in some circumstances.

拥抱影子 2024-08-10 18:52:58

最好的选项是使用标准库中或 CPAN 中的某个模块来为您完成这项工作。它将更加便携,对于快速任务甚至可能更快(无需分叉系统)。

但是,如果这对您来说还不够好,您可以使用这三个之一,不,它们不一样。阅读 perldoc 页面 system(), exec()反引号 以查看差异。

The best option is to use some module, either in the standard library or from CPAN, that does the job for you. It's going to be more portable, and possibly even faster for quick tasks (no forking to the system).

However, if that's not good enough for you, you can use one of those three, and no, they are not the same. Read the perldoc pages on system(), exec(), and backticks to see the difference.

时光礼记 2024-08-10 18:52:58

调用system通常是一个错误。例如,

system "mv $foo /tmp" == 0
    or die "could not move $foo to /tmp";

system "cp $foo /tmp" == 0
    or die "could not copy $foo to /tmp";

您应该说

use File::Copy;

move $foo, "/tmp"
    or die "could not move $foo to /tmp: $!";

copy $foo, "/tmp"
    or die "could not copy $foo to /tmp: $!";

Look on CPAN 来查找为您处理其他命令的模块,而不是说。如果您发现自己编写了大量对 system 的调用,那么可能是时候退回到 shell 脚本中了。

Calling system is generally a mistake. For instance, instead of saying

system "mv $foo /tmp" == 0
    or die "could not move $foo to /tmp";

system "cp $foo /tmp" == 0
    or die "could not copy $foo to /tmp";

you should say

use File::Copy;

move $foo, "/tmp"
    or die "could not move $foo to /tmp: $!";

copy $foo, "/tmp"
    or die "could not copy $foo to /tmp: $!";

Look on CPAN for modules that handle other commands for you. If you find yourself writing a lot of calls to system, it may be time to drop back into a shell script instead.

谁对谁错谁最难过 2024-08-10 18:52:58

好吧,人越多,答案就越多。

我的答案是通常避免外部命令执行。如果可以的话 - 使用模块。执行“cp”、“mv”和许多其他命令是没有意义的——存在执行这些操作的模块。使用模块的好处是它们通常可以跨平台工作。虽然您的系统(“mv”)可能不会。

当我没有其他办法,只能调用外部命令时,我更喜欢使用 IPC::运行。这个想法是,所有简单的方法(反引号、qx、system、用管道打开)本质上都是不安全的,并且需要注意参数。

使用 IPC::Run,我运行命令就像使用 system( @ array ),这更加安全,并且我可以使用变量或回调分别绑定到 stdin、stdout 和 stderr,当您遇到必须通过的情况时,这非常酷从长时间运行的代码到外部程序的数据。

此外,它还具有内置的超时处理功能,这不止一次派上用场:)

Well, the more people the more answers.

My answer is to generally avoid external commands execution. If you can - use modules. There is no point executing "cp", "mv" and a lot of another command - there exist modules that do it. And the benefit of using modules is that they usually work cross-platform. While your system("mv") might not.

When put in situation that I have no other way, but to call external command, I prefer to use IPC::Run. The idea is that all simplistic methods (backticks, qx, system, open with pipe) are inherently insecure, and require attention to parameters.

With IPC::Run, I run commands like I would do with system( @array ), which is much more secure, and I can bind to stdin, stdout and stderr separately, using variables, or callbacks, which is very cool when you'll be in situation that you have to pass data to external program from long-running code.

Also, it has built-in handling of timeouts, which come handy more than once :)

混吃等死 2024-08-10 18:52:58

如果您不希望 shell 介入(通常您不这样做)并且等待系统命令是可以接受的,我建议使用 IPC::Run3。它简单、灵活,可以执行执行程序、为其提供输入并正确捕获其输出和错误流的常见任务。

If you don't want the shell getting involved (usually you don't) and if waiting for the system command is acceptable, I recommend using IPC::Run3. It is simple, flexible, and does the common task of executing a program, feeding it input and capturing its output and error streams right.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文