用于从Perl中的系统命令输出的文件句柄

syk*_*ker 6 perl ipc pipe stdio filehandle

我在Perl中执行的系统命令的输出是否有文件句柄/句柄?

FMc*_*FMc 12

以下是使用以下3参数形式在脚本和其他命令之间建立管道的示例open:

open(my $incoming_pipe, '-|', 'ls -l')             or die $!;
open(my $outgoing_pipe, '|-', "grep -v '[02468]'") or die $!;

my @listing = <$incoming_pipe>;          # Lines from output of ls -l
print $outgoing_pipe "$_\n" for 1 .. 50; # 1 3 5 7 9 11 ...
Run Code Online (Sandbox Code Playgroud)


Gre*_*ill 1

是的,您可以使用这样的管道:

open(my $pipe, "ls|") or die "Cannot open process: $!";
while (<$pipe>) {
    print;
}
Run Code Online (Sandbox Code Playgroud)

open有关更多信息以及perlipc管道操作的完整说明,请参阅文档。

  • 两个参数的“open”既古老又粗糙(而且有潜在危险)。[使用三参数版本](http://www.modernperlbooks.com/mt/2010/04/ Three-arg-open-migration-to-modern-perl.html) (4认同)