kiz has asked for the wisdom of the Perl Monks concerning the following question:
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?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: What does eval actually do?
by Corion (Patriarch) on Oct 28, 2015 at 10:50 UTC | |
Re: What does eval actually do?
by Anonymous Monk on Oct 28, 2015 at 12:02 UTC | |
A reply falls below the community's threshold of quality. You may see it by logging in. |