On 2011-05-18T22:01:26+01:00, ETHER wrote:
Show quoted text> perl -MTryCatch -wle'my $foo = do { try { die } catch ($e) { return
> "OHHAI" } }; print $foo'
>
> gives:
> Can't return outside a subroutine at -e line 1.
An explicit "return" in a catch block (or in the try block) returns
from the enclosing sub.
e.g.
use TryCatch;
sub foo {
try { die }
catch { return "foo\n" }
warn "got here";
}
print foo();
will print "foo\n" but will not issue a warning. TryCatch's behaviour
here is, as far as I'm concerned, far better than Try::Tiny's.
That said, it would be handy if TryCatch could pass through a try or
catch block's implicit return value. The following does not print "foo
\n", but it would be nicer if it did...
use TryCatch;
sub foo {
my $rv =
try { die }
catch { "foo" };
$rv .= "\n";
return $rv;
}
print foo();
I do actually have a patch that allows the above to work, however it
requires you to always using a semicolon after the final catch block,
and forces scalar context onto your blocks, so you can't return lists
in any sort of useful way.