Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: eval to replace die?

by JavaFan (Canon)
on Oct 04, 2010 at 09:05 UTC ( #863288=note: print w/ replies, xml ) Need Help??


in reply to eval to replace die?

I'm wondering if the following is equivalent?
No, they aren't equivalent. If retrieve dies, then in the first snippet, nothing will happen, as the program has terminated before the reaching the die. In the second snippet, it will print the messages, as the eval captures the die. OTOH, if retrieve doesn't die, but returns a false value, the first snippet will die, showing the message, where as the second snippet, the eval blocks return true, and neither the prints, nor the exits will occur.

Evals do not replace dies. In fact, you would use eval in combination with a die. In fact:

sub sub_that_may_die() { die "Oops!" if something_I_do_not_like_happens; } eval {sub_that_may_die()}
is an exception mechanism. die isn't program termination, it is throwing an exception. And eval captures such exceptions. It's only exceptions that aren't captured by an eval that lead to program termination.


Comment on Re: eval to replace die?
Select or Download Code
Re^2: eval to replace die?
by hsmyers (Canon) on Oct 04, 2010 at 23:27 UTC

    Ignoring the tennis match going on here, your answer of 'not equivalent' raises the obvious question of what is? I will probably upload to CPAN with the problem resolved using Try::Tiny, but I still wonder if there is a relatively simple Perl solution that just hasn't occurred to me?

    --hsm

    "Never try to teach a pig to sing...it wastes your time and it annoys the pig."

      If the problem is that exception objects should be used instead of die(), then Try::Tiny won't solve this at all!

      Try::Tiny provides an easy-to-use alternative to eval - but without using some other module to provide exception objects, you'll still be stuck having to call die() in the catch{} block.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://863288]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2014-04-19 22:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls