Perl 中的系统、反引号和管道有什么区别?

发布于 2024-07-19 00:57:27 字数 374 浏览 3 评论 0原文

Perl 支持三种方式(据我所知)运行外部程序:

system:

   system PROGRAM LIST

如:

system "abc";

反引号如:

`abc`;

通过管道运行如:

open ABC, "abc|";

它们之间有什么区别? 这是我所知道的:

  1. 您可以使用反引号和管道轻松获取命令的输出。
  2. 就是这样(将来的编辑还会有更多内容吗?)

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:

  1. You can use backticks and pipes to get the output of the command easily.
  2. that's it (more in future edits?)

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

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

发布评论

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

评论(4

游魂 2024-07-26 00:57:27
  • system():运行命令并返回命令的退出状态
  • 反引号:运行命令并返回命令的输出
  • 管道:运行命令并允许您使用它们作为句柄

另外,反引号将执行程序的 STDOUT 重定向到变量,系统将其发送到主程序的 STDOUT。

  • system(): runs command and returns command's exit status
  • backticks: runs command and returns the command's output
  • pipes : runs command and allows you to use them as an handle

Also backticks redirects the executed program's STDOUT to a variable, and system sends it to your main program's STDOUT.

策马西风 2024-07-26 00:57:27

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.

  • The system sends its output to standard output (and error)
  • The backticks captures the standard output and returns it (but not standard error)
  • The piped open allows you to capture the output and read it from a file handle, or to print to a file handle and use that as the input for the external command.

There are also modules that handle these details with the cross-platform edge cases.

分开我的手 2024-07-26 00:57:27

系统还返回应用程序的退出值(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).

弃爱 2024-07-26 00:57:27

获取程序的退出状态不限于system()。 当您调用close(PIPE)时,它会返回退出状态,您可以从$?获取所有3个方法的最新退出状态。

另请注意,

readpipe('...')

`...`

Getting the program's exit status is not limited to system(). When you call close(PIPE), it returns the exit status, and you can get the latest exit status for all 3 methods from $?.

Please also note that

readpipe('...')

is the same as

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