Skip Menu |
 

This queue is for tickets about the Error CPAN distribution.

Report information
The Basics
Id: 48883
Status: rejected
Priority: 0/
Queue: Error

People
Owner: Nobody in particular
Requestors: markus.martensson [...] gmail.com
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in: 0.17010
Fixed in: (no value)



Subject: Calling return from within a try block does not leave function
Download (untitled) / with headers
text/plain 1.2k
As discussed on http://www.perlmonks.org/index.pl?node_id=278900 and elsewhere, wrapping parts of a function in a try/catch block will cause any return statements found within to become ineffective - the returned value being stuck as value of the try "operator" rather than passed as return value of the function. use Error qw( :try ); sub test { try { return "Within try"; } catch Error::Simple with { }; return "End of function"; } print test()."\n"; In the above code, I would have liked to se "Within try" returned rather than "End of function". This is ofcourse how eval behaves in perl, but is it not how try/catch behaves in other languages. It is a very error prone behaviour. A problem with evals is that there appears to be no way to determine if a "return" call was made or if the value of the eval is simply the result of the last call to "print", "syswrite" or whatever. A potential solution (ugly, evil etc) would be to locate the B::OPs (or rather B::LISTOP) for "return" calls and wrap them with something to let the calling function ("try") know that an actual return call was made. A "local" scoped bool should do the trick. I have had some success with B::Utils::walkoptree_filtered and B::Generate, but my knowledge of the Perl guts is lacking.
On Thu Aug 20 11:04:47 2009, mmartensson wrote: Show quoted text
> In the above code, I would have liked to se "Within try" returned rather > than "End of function". > > This is ofcourse how eval behaves in perl, but is it not how try/catch > behaves in other languages. It is a very error prone behaviour. > > A problem with evals is that there appears to be no way to determine if > a "return" call was made or if the value of the eval is simply the > result of the last call to "print", "syswrite" or whatever. > > A potential solution (ugly, evil etc) would be to locate the B::OPs (or > rather B::LISTOP) for "return" calls and wrap them with something to let > the calling function ("try") know that an actual return call was made. A > "local" scoped bool should do the trick. I have had some success with > B::Utils::walkoptree_filtered and B::Generate, but my knowledge of the > Perl guts is lacking. >
This is the sort of thing that TryCatch does. I suggest using it instead: http://search.cpan.org/~ash/TryCatch-1.001001/lib/TryCatch.pm -- Paul Evans
Download (untitled) / with headers
text/plain 822b
Vid Fre. Aug. 21 06:06:14 2009, skrev PEVANS: Show quoted text
> On Thu Aug 20 11:04:47 2009, mmartensson wrote:
> > In the above code, I would have liked to se "Within try" returned rather > > than "End of function". > > > > This is ofcourse how eval behaves in perl, but is it not how try/catch > > behaves in other languages. It is a very error prone behaviour. > > > > A problem with evals is that there appears to be no way to determine if > > a "return" call was made or if the value of the eval is simply the > > result of the last call to "print", "syswrite" or whatever. > >
> This is the sort of thing that TryCatch does. I suggest using it instead
: Show quoted text
Thanks. As always, "there's more than one way to do it" and "there's more than one module that does it".


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

Please report any issues with rt.cpan.org to rt-cpan-admin@bestpractical.com.