I have excellent code in Raku:
#!/usr/bin/env perl6
CONTROL {
when CX::Warn {
note $_;
die
}
}
use fatal;
role KeyRequired {
method AT-KEY (\key) {
die "Key {key} not found" unless self.EXISTS-KEY(key);
nextsame
}
}
sub execute ($cmd) {
put $cmd;
my $proc = shell $cmd, :err, :out;
if $proc.exitcode != 0 {
put 'exit code = ' ~ $proc.exitcode;
put 'stderr ' ~ $proc.err.slurp;
put 'stdout ' ~ $proc.out.slurp;
die
}
}
execute "ls *.p6"
I say "excellent" because the Raku version runs a command, returns an exit code, and prints stdout/stderr if needed, and all in an easily-read and easily-understood manner.
Reading through the Perl5 manual for IPC::Run https://metacpan.org/pod/IPC::Run I've come across what appears to be the best Perl5 way of doing this, but I find the methods used there to be much less easily readable and understood than the Raku way of doing things.
Reading through the manual for IPC::Run the best that I can find is:
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use feature 'say';
use autodie qw(:all);
use IPC::Run qw(run timeout);
sub execute {
my $cmd = shift;
my @cat = ('cat', __FILE__); # Raku doesn't need to split the string into an array
run \@cat, \undef, \my $out, \my $err, timeout( 10 ) or die "cat: $?";
if ($out ne '') {
say "\$out = $out";
}
if ($err ne '') {
say "\$err = $err";
}
}
execute("cat " . __FILE__);
execute("cat __Fle"); #intentionally wrong to produce an error
How can I re-write the Perl5 so that it is as easily read and used as the Raku code?