Perl的反引号,系统和exec有什么区别? [英] What's the difference between Perl's backticks, system, and exec?

查看:110
本文介绍了Perl的反引号,系统和exec有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我吗?在Perl中,有什么区别?

Can someone please help me? In Perl, what is the difference between:

exec "command";

system("command");

print `command`;

还有其他方法可以运行shell命令吗?

Are there other ways to run shell commands too?

推荐答案

exec

执行命令,永不返回. 就像函数中的return语句.

exec

executes a command and never returns. It's like a return statement in a function.

如果找不到命令,则exec返回false. 它永远不会返回true,因为如果找到该命令,它将根本不会返回. 返回STDOUTSTDERR或退出命令状态也没有意义. 您可以在 perlfunc 中找到有关它的文档, 因为它是一个功能.

If the command is not found exec returns false. It never returns true, because if the command is found it never returns at all. There is also no point in returning STDOUT, STDERR or exit status of the command. You can find documentation about it in perlfunc, because it is a function.

执行命令,命令完成后继续执行Perl脚本.

executes a command and your Perl script is continued after the command has finished.

返回值是命令的退出状态. 您可以在 perlfunc 中找到有关它的文档.

The return value is the exit status of the command. You can find documentation about it in perlfunc.

执行命令,并且在命令完成后继续执行您的perl脚本.

like system executes a command and your perl script is continued after the command has finished.

system 相反,返回值是命令的STDOUT. qx//等同于反引号. 您可以在 perlop ,因为与systemexec不同,它是运算符.

In contrary to system the return value is STDOUT of the command. qx// is equivalent to backticks. You can find documentation about it in perlop, because unlike system and execit is an operator.

以上所缺少的是一种异步执行命令的方法. 这意味着您的perl脚本和命令将同时运行. 这可以通过 open 完成. 它允许您读取STDOUT/STDERR并写入命令的STDIN. 不过,它取决于平台.

What is missing from the above is a way to execute a command asynchronously. That means your perl script and your command run simultaneously. This can be accomplished with open. It allows you to read STDOUT/STDERR and write to STDIN of your command. It is platform dependent though.

还有几个模块可以简化此任务. 有IPC::Open2IPC::Open3IPC::Run,以及 Win32::Process::Create(如果您在Windows上).

There are also several modules which can ease this tasks. There is IPC::Open2 and IPC::Open3 and IPC::Run, as well as Win32::Process::Create if you are on windows.

这篇关于Perl的反引号,系统和exec有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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