【问题标题】:Filehandle for Output from System Command in PerlPerl 中系统命令输出的文件句柄
【发布时间】:2011-03-15 15:06:42
【问题描述】:

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

【问题讨论】:

    标签: perl ipc pipe stdio filehandle


    【解决方案1】:

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

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

    有关详细信息,请参阅open 的文档,有关管道操作的完整描述,请参阅perlipc

    【讨论】:

    【解决方案2】:

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

    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 ...
    

    【讨论】:

      猜你喜欢
      • 2014-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-20
      相关资源
      最近更新 更多