Perl 中的系统、反引号和管道有什么区别?
Perl 支持三种方式(据我所知)运行外部程序:
system
:
system PROGRAM LIST
如:
system "abc";
反引号如:
`abc`;
通过管道运行如:
open ABC, "abc|";
它们之间有什么区别? 这是我所知道的:
- 您可以使用反引号和管道轻松获取命令的输出。
- 就是这样(将来的编辑还会有更多内容吗?)
Perl supports three ways (that I know of) of running external programs:
system
:
system PROGRAM LIST
as in:
system "abc";
backticks as in:
`abc`;
running it through a pipe as in:
open ABC, "abc|";
What are the differences between them? Here's what I know:
- You can use backticks and pipes to get the output of the command easily.
- that's it (more in future edits?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
system()
:运行命令并返回命令的退出状态另外,反引号将执行程序的 STDOUT 重定向到变量,系统将其发送到主程序的 STDOUT。
system()
: runs command and returns command's exit statusAlso backticks redirects the executed program's STDOUT to a variable, and system sends it to your main program's STDOUT.
perlipc 文档解释了与 Perl 中的其他进程交互的各种方式,并且perlfunc 的开放文档 解释了管道文件句柄。
还有一些模块可以通过跨平台边缘情况处理这些细节。
The perlipc documentation explains the various ways that you can interact with other processes from Perl, and perlfunc's open documentation explains the piped filehandles.
There are also modules that handle these details with the cross-platform edge cases.
系统还返回应用程序的退出值(Windows 中的错误级别)。
管道使用起来有点复杂,因为读取它们并关闭它们会增加额外的代码。
最后,他们有不同的实现,旨在做不同的事情。 使用管道,您可以与执行的应用程序进行通信,而其他命令不允许(很容易)这样做。
system is also returning the exit value of the application (ERRORLEVEL in Windows).
Pipes are a bit more complicated to use, as reading from them and closing them adds extra code.
Finally, they have different implementation which was meant to do different things. Using pipes you're able to communicate back with the executed applications, while the other commands doesn't allow that (easily).
获取程序的退出状态不限于
system()
。 当您调用close(PIPE)
时,它会返回退出状态,您可以从$?
获取所有3个方法的最新退出状态。另请注意,
与
Getting the program's exit status is not limited to
system()
. When you callclose(PIPE)
, it returns the exit status, and you can get the latest exit status for all 3 methods from$?
.Please also note that
is the same as