eval

evalBLOCKevalEXPReval
The eval keyword serves two distinct but related purposes in Perl. These
purposes are represented by two forms of syntax, eval BLOCK and
eval EXPR.
The first form traps runtime exceptions (errors) that would otherwise prove fatal,
similar to the “try block” construct in C++ or Java. The second form
compiles and executes little bits of code on the fly at runtime, and
also (conveniently) traps any exceptions just like the first form. But
the second form runs much slower than the first form, since it must
parse the string every time. On the other hand, it is also more general.
Whichever form you use, eval is the
preferred way to do all exception handling in Perl.
For either form of eval, the
value returned from an eval is the
value of the last expression evaluated, just as with subroutines.
Similarly, you may use the return
operator to return a value from the middle of the eval. The expression providing the return
value is evaluated in void, scalar, or list context, depending on the
context of the eval itself. See
wantarray for more on how the
evaluation context can be determined.
If there is a trappable error (including any produced by the
die operator), eval returns undef and puts the error message (or object)
in $@. If there is no error, $@ is guaranteed ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access