P is for Practical | |
PerlMonks |
perlfunc:evalby gods (Initiate) |
on Aug 24, 1999 at 22:42 UTC ( [id://198]=perlfunc: print w/replies, xml ) | Need Help?? |
evalSee the current Perl documentation for eval. Here is our local, out-dated (pre-5.6) version: eval - catch exceptions or compile code
eval EXPR eval BLOCK
In the first form, the return value of
EXPR is parsed and executed as if it were a little Perl program. The value of the expression (which is itself determined within scalar context) is first parsed, and if there weren't any errors, executed in the context of the current Perl program, so that any variable settings or subroutine and format definitions remain afterwards. Note that the value is parsed every time the eval executes. If
EXPR is omitted, evaluates
In the second form, the code within the BLOCK is parsed only once--at the same time the code surrounding the eval itself was parsed--and executed within the context of the current Perl program. This form is typically used to trap exceptions more efficiently than the first (see below), while also providing the benefit of checking the code within BLOCK at compile time. The final semicolon, if any, may be omitted from the value of EXPR or within the BLOCK. In both forms, the value returned is the value of the last expression evaluated inside the mini-program; a return statement may be also used, just as with subroutines. 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 syntax error or runtime error, or a die() statement is executed, an undefined value is returned by eval(), and Note that, because eval() traps otherwise-fatal errors, it is useful for determining whether a particular feature (such as socket() or symlink()) is implemented. It is also Perl's exception trapping mechanism, where the die operator is used to raise exceptions.
If the code to be executed doesn't vary, you may use the eval-BLOCK form to
trap run-time errors without incurring the penalty of recompiling each
time. The error, if any, is still returned in
# make divide-by-zero nonfatal eval { $answer = $a / $b; }; warn $@ if $@;
# same thing, but less efficient eval '$answer = $a / $b'; warn $@ if $@;
# a compile-time error eval { $answer = }; # WRONG
# a run-time error eval '$answer ='; # sets $@
When using the eval{} form as an exception trap in libraries, you may wish not to trigger any
# a very private exception trap for divide-by-zero eval { local $SIG{'__DIE__'}; $answer = $a / $b; }; warn $@ if $@;
This is especially significant, given that
# __DIE__ hooks may modify error messages { local $SIG{'__DIE__'} = sub { (my $x = $_[0]) =~ s/foo/bar/g; die $x }; eval { die "foo lives here" }; print $@ if $@; # prints "bar lives here" } With an eval(), you should be especially careful to remember what's being looked at when:
eval $x; # CASE 1 eval "$x"; # CASE 2
eval '$x'; # CASE 3 eval { $x }; # CASE 4
eval "\$$x++"; # CASE 5 $$x++; # CASE 6
Cases 1 and 2 above behave identically: they run the code contained in the
variable |
|