Perl Programming/Keywords/eval

From Wikibooks, open books for an open world
Jump to navigation Jump to search
Previous: eq Keywords Next: exec

The eval keyword[edit | edit source]

There are two uses of this keyword. The first form is the so-called "string eval" and executes the EXPRESSION as it were a Perl program. The second form, the code within the BLOCK is parsed once together with the code surrounding the eval itself, and executed within the current Perl program context. This form is typically used to trap exceptions more efficiently than the first, and also checking the code within BLOCK during compile time.

Syntax[edit | edit source]

  eval EXPRESSION
  eval BLOCK

Examples[edit | edit source]

  # This code warns on a divide-by-zero
  eval { $division = $divident/$divisor; }; warn $@ if $@;
  []
  eval { die "I lived here" };
Previous: eq Keywords Next: exec