Control Structures

Summary

if

if (expression) {         # condition
  __________________;
  __________________;
   
}elsif (expression){
  __________________;
  __________________;
   
}elsif (expression){
  __________________;
  __________________;
   
}else{
  __________________;
  __________________;
}

unless

unless (expression){     # condition
  __________________;
  __________________;    # do if false
  __________________;
   
}else{
  __________________;
  __________________;    # do if true
  __________________;
}

while

while (expression) {    # condition
  __________________;
  __________________;  # do if expression is true
  __________________;  # then go back and recheck expression
   
}

until

until (expression) {    # condition
  __________________;
  __________________;  # do if expression is false
  __________________;  # then go back and recheck expression
   
}

foreach

foreach $i (@some_array) {
  __________________;
  __________________;      # iterate over array elements
  __________________;
}
foreach (1 .. $n) {
  __________________;
  __________________;      # repeat $n times
  __________________;
}

for

Example: print all even numbers between 4 and 12:

for ($i = 4; $i <= 12; $i += 2) {
   print "$i\n";
}

Comparison operators

See table.

What is true?

  1. Any string except for "" and "0".
  2. Any number except for 0.
  3. Any non-empty array.


Table of Contents.
Previous