Does changing Perl 6's $*OUT change standard output for child processes?

前端 未结 1 1945
走了就别回头了
走了就别回头了 2021-01-17 14:48

I was playing around with shell and how it acts when I change the standard filehandles in the calling program. Proc says:

$in, $out and $

相关标签:
1条回答
  • 2021-01-17 15:01

    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 anyway
    
    my $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: run
    STDOUT: 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
    
    0 讨论(0)
提交回复
热议问题