Skip Menu |

This queue is for tickets about the TryCatch CPAN distribution.

Report information
The Basics
Id: 68294
Status: open
Priority: 0/
Queue: TryCatch

People
Owner: Nobody in particular
Requestors: ether [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: 1.003000
Fixed in: (no value)



Subject: does not return the result of the final expression
perl -MTry::Tiny -wle'my $foo = do { try { die } catch { return "OHHAI" } }; print $foo' gives: OHHAI 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. Shouldn't TryCatch return the result of its last expression? Otherwise, my $foo = try { something() } isn't really feasible with TryCatch, which is one of the core usecases of a vanilla eval {}.
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.
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 "try" to be replaced with "try scalar" with "try list" and return appropriate values from try and catch blocks: use TryCatchHack; use Data::Dumper; my $rv = try scalar { die } catch { "foo" }; print "$rv\n"; my @rv = try list { die } catch { qw/foo bar/ }; print Dumper(\@rv); However, suffers from an annoying bug such that "wantarray" always returns true inside the blocks. I have a partial workaround for that, but it ain't perfect.