Perl 的反引号、system 和 exec 之间有什么区别? [英] What's the difference between Perl's backticks, system, and 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,因为如果找到该命令,它根本不会返回.返回 STDOUT
、STDERR
或命令的退出状态也没有意义.您可以在 perlfunc
<中找到有关它的文档/a>,因为它是一个函数.
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
<中找到有关它的文档/a>.
The return value is the exit status of the command.
You can find documentation about it in perlfunc
.
like system
执行一个命令,你的 perl 脚本在命令完成后继续.
like system
executes a command and your perl script is continued after the command has finished.
与system
相反,返回值是命令的STDOUT
.qx//
相当于反引号.您可以在 perlop
<中找到有关它的文档/strong>,因为与 system
和 exec
不同,它是一个运算符.
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 exec
it 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::Open2
和 IPC::Open3
和 IPC::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 的反引号、system 和 exec 之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!