我在perl中见过这两种形式的管道open
。
一种是简单的管道打开
open FH,'| command';
其他管道安全打开
open FH,'|-','command';
现在,第二个中的 -
有什么用?他们都写入管道。我知道 -
fork 了新进程。
简单的|
也会创建新进程吗?
我们什么时候应该使用安全管道打开|-
?
最佳答案
没有区别
open my $PIPE, '| command';
对比
open my $PIPE, '|-', 'command';
“安全”打开实际上是
open my $PIPE, '|-', 'program', @one_or_more_args;
该版本保证直接启动程序;没有调用 shell。它还使您不必将参数转换为 shell 文字。换句话说,
open my $FH, '|-', 'program', @one_or_more_args;
类似于
use String::ShellQuote qw( shell_quote );
open my $FH, '|'.shell_quote('program', @one_or_more_args);
但没有 shell(因此浪费的资源更少,您可以获得程序的 PID 而不是 shell,并且您可以知道程序是否因信号而终止)。
不幸的是,没有像系统
那样的带有零参数的程序的语法。
(还有打开我的$PIPE,“|-”
,没有进一步的参数,但那是别的东西。)
关于linux - 管道打开 '|' 和 '|-' 之间的区别(安全管道打开),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15308480/