为什么Perl 6不尝试在shell()中处理非零退出?

bri*_*foy 6 exception perl6

尝试捕获异常:

try die X::AdHoc;
say "Got to the end";
Run Code Online (Sandbox Code Playgroud)

输出显示程序继续:

 Got to the end
Run Code Online (Sandbox Code Playgroud)

如果我尝试使用shell并且命令不以0退出,则try不会捕获它:

try shell('/usr/bin/false');
say "Got to the end";
Run Code Online (Sandbox Code Playgroud)

输出看起来不像是一个例外:

The spawned command '/usr/bin/false' exited unsuccessfully (exit code: 1)
  in block <unit> at ... line ...
Run Code Online (Sandbox Code Playgroud)

这是怎么回事呢?

Eli*_*sen 5

Jonathan Worthington 确实提供了答案:

https://irclog.perlgeek.de/perl6-dev/2017-04-04#i_14372945
Run Code Online (Sandbox Code Playgroud)

简而言之,shell() 返回一个 Proc 对象。该对象沉没的那一刻,如果运行程序失败,它将抛出它内部的异常。

$ 6 'dd shell("/usr/bin/false")'
Proc.new(in => IO::Pipe, out => IO::Pipe, err => IO::Pipe, exitcode => 1, signal => 0, command => ["/usr/bin/false"])
Run Code Online (Sandbox Code Playgroud)

所以,你需要做的是在一个变量中捕获 Proc 对象,以防止它被沉没:

$ 6 'my $result = shell("/usr/bin/false"); say "Got to the end"'
Got to the end
Run Code Online (Sandbox Code Playgroud)

然后你可以使用 $result.exitcode 来查看它是否成功。