Loops and Ifs

You can create a simple for loop like you do in C:

   for ($i = 0; $i < 10; $i++)
   {
      print $i, "\n";
   }

While statements are easy:

   $i = 0;
   while ( $i < 10 )
   {
      print $i, "\n";
      $i++;
   }

If statements are similarly easy:

   for ($i = 0; $i < 10; $i++)
   {
      if ($i != 5)
      {
         print $i, "\n";
      }
   }

The boolean operators work like they do in C:

  • && and
  • || or
  • ! not

For numbers:

  • == equal
  • != not equal
  • <, <=, >, >= (as expected)

Others:

  • eq
  • ne
  • lt
  • le
  • gt
  • ge

If you have an array, you can loop through it easily with foreach:

   @a = ('dog', 'cat', 'eel');
   foreach $b (@a)
   {
      print $b, "\n";
   }

Foreach takes each element of the array @a and places it in $b until @a is exhausted.

PERL Note

You must use the "begin" and "end" braces -- { and } -- even for a single line.