Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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).


In reply to Re: eval and return values by jplindstrom
in thread eval and return values by Anonymous Monk

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2023-12-06 05:38 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (29 votes). Check out past polls.