So what does eval actually do, and actually trap?

Considering this code:

eval { $content = $self->web_fetch( { raw => 1 } ); }; # a $content = eval { $self->web_fetch( { raw => 1 } ); }; # b if ($EVAL_ERROR) { # $@ return $self->do_harvest_return(undef); }

(We can assume that web_fetch and do_harvest_return are methods written by us, and contain some carp and croak statements)

#1 There's an implicit statement that eval does not return true or false on it's own... the implication is that it doesn't return anything (unlike, for example, print - which returns true on a successfull print)

I assume $EVAL_ERROR will contain the first croak (or other fatal) error from the web_fetch method in both a and b

.... but what about the difference between a and b - doesn't a also report if the assignment to $content fails? - if so, why does best practice suggest b? (2nd Edition, P 277)

#2 Is there a preferred style for using eval?



-- Ian Stuart
A man depriving some poor village, somewhere, of a first-class idiot.

In reply to What does eval actually do? by kiz

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.