代码之家  ›  专栏  ›  技术社区  ›  jjmerelo

使用Proc::Async从绑定管道读取

  •  9
  • jjmerelo  · 技术社区  · 7 年前

    Proc::Async 是Perl 6用于与系统异步交互的类之一。文件规定了这种方式 to bind to the output of an external program :

    my $p = Proc::Async.new("ls", :out);
    my $h = "ls.out".IO.open(:w);
    $p.bind-stdout($h);
    await $p.start;
    
    say "Done";
    

    (添加了一些修改,如等待承诺)。但是,我不知道如何打印这个的输出 $p .添加 tap 产生此错误:

    Cannot both bind stdout to a handle and also get the stdout Supply
    

    在bind stdout的块中。p6第8行

    print and write methods 在文档中,但我不知道如何 read 而不是读取文件。有什么想法吗?

    1 回复  |  直到 7 年前
        1
  •  9
  •   nxadm    7 年前

    我不确定你能做到这一点(错误非常明显)。作为一种解决方法,您可以定期点击并打印到标准输出和同一块中的文件:

    my $p = Proc::Async.new("ls", :out);
    my $h = "ls.out".IO.open(:w);
    $p.stdout.tap(-> $str { print $str; $h.print($str) });
    await $p.start;
    
    say "Done";