Prev

Perl Notes

Next

Chapter 6.1 Test Constructs

A condition is either true or false.

if
A conditional statement has three principal parts: the conditional word ("if") that determines how the condition will be analyzed; the condition itself, which is generally enclosed in parentheses; and a block, which begins with an opening curly bracket, then contains one or more statements, and ends with a closing curly bracket.

if ($food eq "spinich") {
  print "You ate your spinich. Have some desert.\n";
}

if/else
The else caluse lets you add a statement that will be executed only if the condition is false. If the condition is true, the statement in the if block are executed as usual.

if ($food eq "spinich") {
  print "You ate your spinich. Have some desert.\n";
} else {
  print "No spinanch, no dessert!\n";
}

if/elsif
If the condition in the initial is block is false, each elsif's condition is evaluated in order. If one turns out to be true, its statements are then executed and the rest of the if structure is ignored. If no conditions are true, the else block is executed.

if ($food eq "spinich") {
  print "You ate your spinich. Have some desert.\n";
} elsif ($food eq "broccoli") {
  print "Okay, maybe I'll let you have desert.\n";
} else {
  print "No spinanch, no dessert!\n";
}

unless
The unless condition works alone, without else or elsif and only executes if the condition is false.

unless ($food eq "spinich") {
  print "No spinanch, no dessert!\n";
}


Prev Home Next