Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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";

(Some modifications added, like awaiting for the promise). However, I don't know how to print the output of this $p. Adding a tap yields this error:

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

in block at bind-stdout.p6 line 8

There are print and write methods in the documentation, but I don't know how to read from it other than reading the file. Any idea?

like image 753
jjmerelo Avatar asked May 13 '18 07:05

jjmerelo


1 Answers

I am not sure you can do that (the error is very explicit). As a workaround you could get a regular tap and print to stdout and a file in the same block:

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";
like image 174
nxadm Avatar answered Nov 01 '22 18:11

nxadm