=over =item eval EXPR X X X X X X X X =item eval BLOCK =item eval 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 lexical 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 C executes. If EXPR is omitted, evaluates C<$_>. This form is typically used to delay parsing and subsequent execution of the text of EXPR until run time. In the second form, the code within the BLOCK is parsed only once--at the same time the code surrounding the C 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 C itself. See L for more on how the evaluation context can be determined. If there is a syntax error or runtime error, or a C statement is executed, C returns an undefined value in scalar context or an empty list in list context, and C<$@> is set to the error message. If there was no error, C<$@> is guaranteed to be the empty string. Beware that using C neither silences Perl from printing warnings to STDERR, nor does it stuff the text of warning messages into C<$@>. To do either of those, you have to use the C<$SIG{__WARN__}> facility, or turn off warnings inside the BLOCK or EXPR using S>. See L, L, L and L. Note that, because C traps otherwise-fatal errors, it is useful for determining whether a particular feature (such as C or C) is implemented. It is also Perl's exception trapping mechanism, where the die operator is used to raise exceptions. If you want to trap errors when loading an XS module, some problems with the binary interface (such as Perl version skew) may be fatal even with C unless C<$ENV{PERL_DL_NONLAZY}> is set. See L. 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 C<$@>. Examples: # 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 $@ Using the C form as an exception trap in libraries does have some issues. Due to the current arguably broken state of C<__DIE__> hooks, you may wish not to trigger any C<__DIE__> hooks that user code may have installed. You can use the C construct for this purpose, as this example shows: # a private exception trap for divide-by-zero eval { local $SIG{'__DIE__'}; $answer = $a / $b; }; warn $@ if $@; This is especially significant, given that C<__DIE__> hooks can call C again, which has the effect of changing their error messages: # __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" } Because this promotes action at a distance, this counterintuitive behavior may be fixed in a future release. With an C, 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 $x. (Although case 2 has misleading double quotes making the reader wonder what else might be happening (nothing is).) Cases 3 and 4 likewise behave in the same way: they run the code C<'$x'>, which does nothing but return the value of $x. (Case 4 is preferred for purely visual reasons, but it also has the advantage of compiling at compile-time instead of at run-time.) Case 5 is a place where normally you I like to use double quotes, except that in this particular situation, you can just use symbolic references instead, as in case 6. The assignment to C<$@> occurs before restoration of localised variables, which means a temporary is required if you want to mask some but not all errors: # alter $@ on nefarious repugnancy only { my $e; { local $@; # protect existing $@ eval { test_repugnancy() }; # $@ =~ /nefarious/ and die $@; # DOES NOT WORK $@ =~ /nefarious/ and $e = $@; } die $e if defined $e } C does I count as a loop, so the loop control statements C, C, or C cannot be used to leave or restart the block. An C executed within the C package doesn't see the usual surrounding lexical scope, but rather the scope of the first non-DB piece of code that called it. You don't normally need to worry about this unless you are writing a Perl debugger. =back