使用 Proc::Async 从绑定管道读取
Reading from a bound pipe using Proc::Async
Proc::Async
is one of the classes that Perl 6 uses for interacting asynchronously with the system. The documentation specifies this way 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
从中读取文件。有什么想法吗?
我不确定你能做到(错误非常明显)。作为一种解决方法,您可以定期点击并打印到标准输出和同一块中的文件:
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";
Proc::Async
is one of the classes that Perl 6 uses for interacting asynchronously with the system. The documentation specifies this way 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
从中读取文件。有什么想法吗?
我不确定你能做到(错误非常明显)。作为一种解决方法,您可以定期点击并打印到标准输出和同一块中的文件:
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";