Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: Try::Tiny - return in catch from sub

by mjscott2702 (Pilgrim)
on Nov 26, 2010 at 14:06 UTC ( #873870=note: print w/replies, xml ) Need Help??

in reply to Try::Tiny - return in catch from sub

The doc for that module says it supports a return from the "try" block, doesn't mention the "catch" block - you could try setting a flag inside the catch (defined at the sub level) and return if that flag is set e.g.

sub foo { my $error = 0; try { die; } catch { print "error\n"; $error = 1; } return if $error; print "no_err\n"; }

I agree from a program flow point-of-view, it would be better if the return inside the catch worked as expected - it may be that the "catch" is itself similar to a sub, and the return is returning from that?

Replies are listed 'Best First'.
Re^2: Try::Tiny - return in catch from sub
by Anonymous Monk on Nov 26, 2010 at 15:03 UTC
    um, it says what try returns, what catch returns ... see this example
    try { die_sometimes(); } catch { # ...code run in case of error } finally { if (@_) { print "The try block died with: @_\n"; } else { print "The try block ran without error.\n"; } };
    if you want to return from your sub from within a try/catch then use as directed
    use TryCatch; sub foo { my ($self) = @_; try { die Some::Class->new(code => 404 ) if $self->not_found; return "return value from foo"; } catch (Some::Class $e where { $_->code > 100 } ) { } }
    Try::Tiny is for when TryCatch is too much, it is not a drop in replacement, mind the caveats
      um, not in the CPAN documentation:

      This module provides bare bones try/catch/finally statements that are designed to minimize common mistakes with eval blocks, and NOTHING else. This is unlike TryCatch which provides a nice syntax and avoids adding another call stack layer, and supports calling return from the try block to return from the parent subroutine.

        That says Try::Tiny does not support returning from try/catch/finally blocks like TryCatch, its how they're different

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://873870]
[choroba]: because, someone renamed the method, but didn't notice it was used in the test, as the test was skipped
[marinersk]: Well, if the method doesn't exist, it would be hard to pass the test.
[choroba]: later, someone removed the new method, as all its usage places were safe, but didn't notice the test still used the old name
[choroba]: fortunately, it wasn't that hard to replace the method and fix a few remaining failures due to the changes we did to the codebase over the years
[marinersk]: choroba Sounds like a process improvement opportunity; tests may not all need to be run, but they should all be compiled with perl -c before check-in/promotion happens.
[choroba]: so, now I have the test, so I can start making changes in the code. Back to the original ticket, yay!
[marinersk]: I definitely love it when my $current_taask=& taskPop(); if ($current_task eq $original_task) { } is true.
[marinersk]: s/taask/task/g;

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (11)
As of 2017-05-25 15:16 GMT
Find Nodes?
    Voting Booth?