< Perl Programming < Keywords

The eval keyword

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

  eval EXPRESSION
  eval BLOCK

Examples

  # This code warns on a divide-by-zero
  eval { $division = $divident/$divisor; }; warn $@ if $@;
  []
  eval { die "I lived here" };
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.