Does changing Perl 6's $*OUT change standard output for child processes?
By default the IO::Handle that is in $*OUT
is bound to the low-level STDOUT filehandle given by the operating system.
shell
and run
just let the spawned process use the low-level STDOUT file that was given to PerlĀ 6, unless you specify otherwise.
PerlĀ 6 doesn't change anything about the outside environment until the moment before it spawns a new process.
The simplest thing to do is to give the filehandle object you want to use to the shell
or run
call with a named argument.
# no testing for failure because the default is to throw an error anywaymy $p6-name = 'in-out.p6'.IO;END $p6-name.unlink;$p6-name.spurt(Q'put "STDOUT: @*ARGS[0]";note "STDERR: @*ARGS[0]"');run $*EXECUTABLE, $p6-name, 'run', :out(open '/dev/null', :w);{ temp $*OUT = open '/dev/null', :w; shell "'$*EXECUTABLE' '$p6-name' 'shell'", :err($*OUT);}
This results in
STDERR: runSTDOUT: shell
In the particular case of throwing away the output data, :!out
or :!err
should be used instead.
run $*EXECUTABLE, $p6-name, 'no STDERR', :!err;
STDOUT: no STDERR
If you just want the data to be intercepted for you :out
and :err
do just that;
my $fh = run( $*EXECUTABLE, $p6-name, 'capture', :out ).out;print 'captured: ',$fh.slurp-rest;
captured: STDOUT capture