Control Structures

Like other languages, Perl has a number of control structures:

Comparison Operators

 

First, let's just make sure the comparison operators are known:

 

$a = 1;
$b = 2;
$c = 2;
$d = "Hello";
$e = "Hello";
$f = "Goodbye";

# Comparisons - True
if ($b == $c) {print "True\n";} else {print "False\n";}
if ($a != $b) {print "True\n";} else {print "False\n";}
if ($d eq $e) {print "True\n";} else {print "False\n";}
if ($a && $b) {print "True\n";} else {print "False\n";}
if ($a || $b) {print "True\n";} else {print "False\n";}
if ($a)       {print "True\n";} else {print "False\n";}

# Comparisons - False
if ($b != $c) {print "True\n";} else {print "False\n";}
if ($a == $b) {print "True\n";} else {print "False\n";}
if ($d ne $e) {print "True\n";} else {print "False\n";}
if ($a && 0)  {print "True\n";} else {print "False\n";}
if (0 || 0)   {print "True\n";} else {print "False\n";}
if (!($a))    {print "True\n";} else {print "False\n";}

 

So, as an explanation:

The for statement

 

The for statement is used in a very similar way to C/C++:

 

for ($a = 0; $a <= 10; $a ++)
{
  print "Number is $a\n";
}

 

The first part defines the variable to use and sets the starting value, the second part the "continuation" condition (i.e. this must be true for the loop to continue), and the third part is the statement that is executed on each iteration of the loop.

 

The foreach statement

 

This one won't be familiar to C/C++ users.  It's used to iterate through each item in an array:

 

@MyArray = (1, 5, 10, 20);
foreach $Number (@MyArray)
{
  print "Number is $Number\n";
}

 

You can use this on a hash as well, but it will be handled in the list context, so each the first assignment will be the key, then the value, then the next key etc.

 

As a point of interest, the foreach statement is a synonym for the for statement, so you can use either in the foreach loop.  Also, you can define the variable to be visible only within the loop by using the my statement (more information in the subroutines section):

 

@LIST = (1, 2, 3, 4, 5);
for my $NUMBER (@LIST)      # for can be used in place of foreach
{
  print $NUMBER . "\n";     # $NUMBER can only be "seen" within this block because of the "my" statement
}

 

Have a look at the end of this section to see an example of the difference between C/C++ style implementation and Perl implementation with a foreach loop!

 

The while / until (and do) statements

 

Another common loop statement is the while or until statements.  They are used as follows:

 

$a = 5;
while ($a != 0)
{
  print "Value is $a\n";
  $a--;
}

$a = 5;
until ($a == 0)
{
  print "Value is $a\n";
  $a--;
}

 

Both of the above examples result in the same output (the numbers from 5 to 1).  The statement in the block is only executed if the condition is true for at least one iteration ("pre-testing"), whereas is you use the do statement, you can "post-test" and the block contents will be executed irrespective of the condition at least once:

 

$a = 5;
do
{
  print "Value is $a\n";
  $a--;
}
while ($a != 0);

$a = 5;
do
{
  print "Value is $a\n";
  $a--;
}
until ($a == 0);

 

As it happens, the result of these two examples is again identical to the ones at the top.

 

Conditional Statements

 

Perl obviously has some conditional statements as well (otherwise things get a bit difficult!).  You've met most of these before in the examples, but for an example:

 

$a = 1;
$b = 2;

if ($a == $b)
{
  print "Equal\n";
}
elsif ( ($a + 1) == $b)
{
  print "B = A + 1\n";
}
else
{
  print "Some other condition\n";
}

 

So, the if statement has a condition which is contained in parentheses and must evaluate to true in order to execute the block statement (incidentally, true is non zero, a non empty, or some other non-null value string in Perl).  Elsif and else are used as alternatives to the initial statement.

 

C/C++ style implementation compared to Perl

 

Just as an arbitrary show of how you can use Perl, this shows C/C++ style implementation of an algorithm:

 

# Define two arrays of numbers
@Array1 = (1, 5, 10, 20, 30);
@Array2 = (20, 5, 2, 1);

# Here is an algorithm
for (my $i = 0; $i < @Array1; $i++)
{
  for (my $j = 0; $j < @Array2; $j++)
  {
    if ($Array1[$i] > $Array2[$j])
    {
      last; # can't go to outer :-(
    }
    $Array1[$i] += $Array2[$j];
  }
  
  # this is where that last takes me
}

print "@Array1\n";
print "@Array2\n";

 

And the same algorithm implemented in a Perl style:

 

# Define two arrays of numbers
@Array1 = (1, 5, 10, 20, 30);
@Array2 = (20, 5, 2, 1);

OUTER: for my $OUTER (@Array1)
{
  INNER: for my $INNER (@Array2)
  {
     next OUTER if $OUTER > $INNER;
    $OUTER += $INNER;
  }
}

print "@Array1\n";
print "@Array2\n";

 

Much nicer, huh?! 

 

Page Last Updated on Wednesday September 04, 2002