【问题标题】:Does changing Perl 6's $*OUT change standard output for child processes?更改 Perl 6 的 $*OUT 是否会更改子进程的标准输出?
【发布时间】:2017-07-19 05:03:37
【问题描述】:

我在玩shell 以及当我在调用程序中更改标准文件句柄时它的作用。 Proc 说:

$in、$out、$err是待启动程序的三个标准流,默认为“-”,表示继承父进程的流。

据我所知,外部程序不使用相同的文件句柄:

#!/Applications/Rakudo/bin/perl6

#`(
    make an external Perl 6 program the outputs to standard handles
    )
my $p6-name = 'in-out.p6'.IO;
#END try $p6-name.unlink; # why does this cause it to fail?
my $p6-fh = open $p6-name, :w;
die "Could not open $p6-name" unless ?$p6-fh;
$p6-fh.put: Q:to/END/;
    #!/Applications/Rakudo/bin/perl6

    $*ERR.say( qq/\t$*PROGRAM: This goes to standard error/ );
    $*OUT.say( qq/\t$*PROGRAM: This goes to standard output/ );
    END
$p6-fh.close;
say $p6-name.e ?? 'File is there' !! 'File is not there';
die "$p6-name does not exist" unless $p6-name.e;

{
#`(
    Start with some messages to show that we can output to
    the standard filehandles.
    )
$*OUT.put: "1. standard output before doing anything weird";
$*ERR.put: "2. standard error before doing anything weird";
shell( "perl6 $p6-name" ).so;
}

{
#`(
    This block assigns a new filehandle to $*OUT and prints a
    message to it. I expect that message to not show up in the
    terminal.

    It then calls run-them to fire off the external process. It
    should inherit the same standard out and its standard out
    messages should not show up. But, they do.
    )
temp $*OUT = open '/dev/null', :w;
$*OUT.put: "3. temp redefine standard output before this message";
shell( "perl6 $p6-name" ).so;
}

$*OUT.put: "4. everything should be back to normal";

输出显示,当我打开 /dev/null 并将其文件句柄分配给 $*OUT 时,当前程序的输出不会显示在终端中(没有以 @ 开头的输出987654326@)。但是,当我调用shell 时,它的标准输出会转到原来的标准输出:

File is there
1. standard output before doing anything weird
2. standard error before doing anything weird
    in-out.p6: This goes to standard error
    in-out.p6: This goes to standard output
    in-out.p6: This goes to standard error
    in-out.p6: This goes to standard output
4. everything should be back to normal

我并不担心如何做到这一点。我可以创建一个Proc 对象并将文件句柄传递给它。

还有其他事情吗?

【问题讨论】:

  • MoarVM中的相关代码好像在MVM_proc_shell。在 Windows 上,第一次运行脚本时,它找不到刚刚创建的文件。在第二次运行时,我观察到相同的行为(在将 /dev/null 替换为 NUL 之后)。

标签: shell stdout raku


【解决方案1】:

默认情况下,$*OUT 中的 IO::Handle 绑定到操作系统提供的低级 STDOUT 文件句柄。

shellrun 只是让生成的进程使用提供给 Perl 6 的低级 STDOUT 文件,除非您另有指定。

Perl 6 在产生新进程之前不会改变任何关于外部环境的内容。


最简单的做法是将要使用的文件句柄对象提供给带有命名参数的shellrun 调用。

# 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);
}

这会导致

STDERR: run
STDOUT: shell

在丢弃输出数据的特殊情况下,应使用:!out:!err

run $*EXECUTABLE, $p6-name, 'no STDERR', :!err;
STDOUT: no STDERR

如果您只想为您截取数据 :out:err 就这样做;

my $fh = run( $*EXECUTABLE, $p6-name, 'capture', :out ).out;
print 'captured: ',$fh.slurp-rest;
captured: STDOUT capture

【讨论】:

  • 好的,但是 Proc 文档说 shell 继承了父进程的流。文档中是否有某处讨论“在 Perl 6 中基本上修改除 %*ENV 之外的任何变量都没有外部影响。”?这是设计目标还是实施问题?
猜你喜欢
  • 2014-07-24
  • 1970-01-01
  • 1970-01-01
  • 2011-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-28
  • 2021-08-13
相关资源
最近更新 更多