The "or die" Idiom

The or, and || operators short circuit. If what is on the left is true, then what is on the right is never evaluated, because it doesn't need to be.


  $a = 10;
  $b = 99
  $a < 100 or $b < 100;  # $b comparison never evaluated


The die() Function Aborts Execution with an Error Message


die "\$a is the wrong size" unless ($a < 100 and $a > 0);

You Combine them Idiomatically Like This


($a < 100 and $a > 0) or die "\$a is the wrong size";

You can use "and" in the Same Way

If what is on the left of the "and" is FALSE, then Perl doesn't evaluate what's on the right, because it doesn't need to.

$a < 0 and die "\$a is negative";


<< Previous
Contents >> Next >>

Lincoln D. Stein, lstein@cshl.org
Cold Spring Harbor Laboratory
Last modified: Wed Oct 11 20:53:55 EDT 2000