|
|
|
|
|
by simcop2387
3348 days ago
|
|
So with perl 5 there isn't a real exception handler like you'd expect from other languages (I'm unsure of perl 6). It works like the following (program output available at [1]): use v5.24.0;
my $x = 1;
my $y = "bar";
sub foo { # this sub fails fatally
die "what is going on here";
}
say "x is $x, y is $y"; # 1 and bar
eval { # similar to try
my $y = "baz"; # we have a new lexical scope in here too.
$x = 2; # but this will change the outside scope since it's also available to us
say "x is $x, y is $y"; # 2 and baz
foo();
$x = 3; # this never happens
};
if ($@) { # check for an error, similar to catch
say "CAUGHT ERROR: ", $@; # just parrot the error out for now.
}
say "x is $x, y is $y"; # 2 and bar
Inside the eval the lexical scope outside is still available and a new one is also available. The error that happens gets stuffed into a special variable and you can choose to handle it or not later. This works the same way if you give eval a string to run also.[1] https://perlbot.pl/pastebin/pl5u9w |
|