Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: eval and return values

by jplindstrom (Monsignor)
on Jan 14, 2009 at 12:44 UTC ( [id://736216]=note: print w/replies, xml ) Need Help??


in reply to eval and return values

First, remove the "my" inside the eval block, otherwise it won't do what you think it does.

To me, as written, it's equivalent to:

return bar->something();

That's pretty succinct :)

What is it that you really want to do? Log the error before rethrowing it?

Having said that, this is a slightly more succinct way:

my $foo = eval { bar->something() }; die if $@; # See "perldoc -f die" for what this does
But note that if you call a method in between the eval and checking $@, any eval potentially called inside the method will reset $@ for you, so you need to capture the value of $@ immediately.

Also note that other things may potentially mess up $@ for you (if an eval was used in a DESTROY block which was triggered by the exception). This is normally not a problem, but when something in your code changes and it becomes a problem it will confuse you for a bit because of the action at a distance.

So overall it's better to make sure the eval returns a true value, and check for that. If an exception is thrown, eval returns undef (perldoc -f eval).

/J

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-25 20:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found