Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I store the value returned by either run or shell?

Tags:

raku

Let's say I have this script:

# prog.p6
my $info = run "uname";

When I run prog.p6, I get:

$ perl6 prog.p6
Linux

Is there a way to store a stringified version of the returned value and prevent it from being output to the terminal?

There's already a similar question but it doesn't provide a specific answer.

like image 701
Luis F. Uceta Avatar asked Dec 04 '22 19:12

Luis F. Uceta


1 Answers

You need to enable the stdout pipe, which otherwise defaults to $*OUT, by setting :out. So:

my $proc = run("uname", :out);
my $stdout = $proc.out;
say $stdout.slurp;
$stdout.close;

which can be shortened to:

my $proc = run("uname", :out);
say $proc.out.slurp(:close);

If you want to capture output on stderr separately than stdout you can do:

my $proc = run("uname", :out, :err);
say "[stdout] " ~ $proc.out.slurp(:close);
say "[stderr] " ~ $proc.err.slurp(:close);

or if you want to capture stdout and stderr to one pipe, then:

my $proc = run("uname", :merge);
say "[stdout and stderr] " ~ $proc.out.slurp(:close);

Finally, if you don't want to capture the output and don't want it output to the terminal:

my $proc = run("uname", :!out, :!err);
exit( $proc.exitcode );
like image 128
ugexe Avatar answered Jan 20 '23 13:01

ugexe