I'd like to send a string to a subprocess's standard input stream, as I might do in Perl 5 like this:
pipe *READ, *WRITE;
if (my $pid = fork()) {
close READ;
print WRITE "Hello world!\n";
close WRITE;
} else {
close WRITE;
open STDIN, '<&=' . fileno READ;
close READ;
exec 'cat';
}
What's the equivalent construction in Raku?
I can send any input to a an external cat
process to get it back in a stream I can pass to yet another process, but that's just terrible.
I've found this Raku module that seems relevant, but it has "Input as well as output" as a TODO, so it doesn't help me now.
There's also this question on this very site that matches this one closely. That author asked for a solution that doesn't rely on an external process, as I am, but the accepted solution still uses an external process.
Finally, I note the docs for Raku's IO::Pipe
class, which seems like it should fit the bill. However, that page says:
Pipes can be easily constructed with sub run and Proc::Async.new.
...which I explicitly want to avoid. I tried just creating a new IO::Pipe
object, hoping I could use its in
and out
streams in a natural way, but I get:
> my $pipe = IO::Pipe.new
Required named parameter 'on-close' not passed
in block <unit> at <unknown file> line 1
That parameter is not mentioned on the doc page, so I suppose I've ventured into undefined territory here.
A Raku program consists of zero or more statements. A statement ends with a semicolon or a curly brace at the end of a line. In Raku, single line comments start with a single hash character # and extend until the end of the line. Raku also supports multi-line/embedded comments.
A statement ends with a semicolon or a curly brace at the end of a line. In Raku, single line comments start with a single hash character # and extend until the end of the line. Raku also supports multi-line/embedded comments. The compiler doesn't evaluate comments as program text and they're only intended for human readers.
The program takes advantage of this property of Raku's sort to achieve the goal by sorting twice: first by the number of sets won (the secondary criterion), then by the number of matches won (the primary criterion). After the first sorting step, the names are in the order Beth Charlie Dave Ana.
Double quoted strings in Raku can interpolate variables with the $ sigil as well as blocks of code in curly braces. Since any arbitrary Raku code can appear within curly braces, Array s and Hash es may be interpolated by placing them within curly braces.
So this turned out to be embarrassingly easy. Just provide a boolean True
value for run
's in
parameter, then access the resulting process's in
field and write to it.
my $proc = run 'my-command', 'and', 'args', :in;
$proc.in.say('Hello world!');
$proc.in.close;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With