7

This try catches the exception:

try die X::AdHoc;
say "Got to the end";

The output shows that the program continues:

 Got to the end

If I attempt it with shell and a command that doesn't exit with 0, the try doesn't catch it:

try shell('/usr/bin/false');
say "Got to the end";

The output doesn't look like an exception:

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

What's going on that this makes it through the try?

Elizabeth Mattijsen
  • 25,654
  • 3
  • 75
  • 105
brian d foy
  • 129,424
  • 31
  • 207
  • 592

1 Answers1

5

The answer is really provided by Jonathan Worthington:

https://irclog.perlgeek.de/perl6-dev/2017-04-04#i_14372945

In short, shell() returns a Proc object. The moment that object is sunk, it will throw the exception that it has internally if running the program failed.

$ 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"])

So, what you need to do is catch the Proc object in a variable, to prevent it from being sunk:

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

And then you can use $result.exitcode to see whether it was successful or not.

Elizabeth Mattijsen
  • 25,654
  • 3
  • 75
  • 105
  • 1
    And if explosion is wanted, one can do `try sink shell 'false'` to sink the `Proc` *inside* the `try`. –  Apr 04 '17 at 12:19