Lesson 7 - Flow Control
Flow Control

Flow Control in any programming language is very important. Establishing flow control is the only way you can develop a
program with more than one function. Without flow, you can't test things and carry out actions based on the results of the
test. The only program you can create without some kind of flow would be a program that simply outputs some text and exits
(like "Hello World".)


If

The most basic test you can use is the `if` statement. The commands in an `if` block will execute only if the test you are
making evaluates to true. An example is below:

$a = 0;
$b = 1;

if ($a == $b) {
print '$a is equal to $b'; # Note that we used `'` characters instead of `"` characters (see "Print" lesson.)
# This causes for `$a` to be printed rather than `0` and the same for `$b`.
}

In this case the if statement can not evaluate to true becuase `$a` (which is `0`) can never equal `$b` (`1`). If you made
`$b` also equal to `0` just like `$a` then the if statement would evaluate to true and the contents would be printed.

A more advanced use of `if` is `elsif` and `else`. An example is below.

$a = "Test String";

if ($a eq "Not a test") {
# Do something.
} elsif ($a eq "Another Test") {
# Do something.
} elsif ($a eq "Test String") {
# Do something.
} else {
# Do something.
}

In this example `$a` will go through each test until it gets to one that is true. If it can't find any true statements the
`else` block will evaluate to true.

Note that we used `eq` commands instead of `==` commands in the tests. This is becuase we are testing strings and not
integers. More on this in the next topic.

 

Tests

$a = 1;
if ($a = 0) {
print '$a is zero.';
}

In this example your program output will be `$a is zero.` simply because you used `=` instead of `==` in your test. Using a
single `=` sign tells perl you want to assign a vaule to something. In the above sample the assignment was valid, and
therefore returned true. In order to test `$a` you need to use `==`. You can also use an array of other tests:

Test Meaning
-----------
`eq` String Equal
`ne` String Not Equal
`==` Integer Equal
`!=` Integer Not Equal
`>=` Integer Greater than or Equal to
`<=` Integer Less than or Equal to


While

The while statement will do something while a statement is true:

$a = 0;

while ($a != 10) {
$a++; # Increment $a by one.
}

This look will increment `$a` while `$a` is not equal to `10`.

A very useful application of the while statement is with File I/O (see File I/O topic.) You can use statements like this:

while (<FILEHANDLE>) {
print $_; # $_ is a special variable and will in this case be the line in the file.
}

This block of commands will execute printing out `$_` (which contains a line from the file) while there are more lines in
the file. Once it has looped thru all of the lines in the file it will stop returning lines and the while statement will
therefore be false and not process the commands inside of it.


Unless

The `unless` command is just like the `if` command inverted. You can use statements like:

unless ($a eq "Test") {
# Do something.
}

This will run the command inside the `unless` block unless `$a` is equal to `Test`.


Foreach

Foreach loops will run the loop for each value cited. An example follows:

foreach $value (@in) {
print "$value\n"; # print the value with a newline character on the end.
}

This example will print out each value in the array `@in`. You can also drop the $value part of this statement and the values
will be inserted into `$_` by perl. This means you could use this command to do the same as above:

foreach (@in) {
print "$_\n";
}

This is shorter but you can never be gaurenteed that `$_` will not change on you somewhere when you don't expect it to change.
`$_` is a special variable that can be used in place of an actual variable. For more information see the variables topic.



Subs

Subroutines in perl are defined with the keyword `sub`. You can create a sub using:

sub mysub {
# Do something.
}

By executing `&mysub();` the sub will be executed and after execution control returned to the next command after you called
the sub.

Subs can also returns values, in which case they become a function. To use this feature of perl call your sub like
`$returnfromsub = &mysub();` and whatever the sub returns will be put into `$returnfromsub`. To return a value from a sub use
the `return` command.

An example:

$ret = &returnsub();
print $ret;

sub returnsub
{
return "My String";
}

The above example will print out the contents of `$ret`, which are `My String` after it gets set from the results `returnsub`.

Data can also be passed to a sub in perl. You can use this by calling the sub like `&mysub($passthis);`. This will pass the
contents of $passthis into the `@_` special array for the sub. The sub can access each value of this array.

An full example of evertyhing we have learned about the use of subroutines:

$printthis = "Print This String\n";
$ret = &returnsub($printthis);
print $ret;

sub returnsub
{
return @_[0]; # Return the value of the 1st element in the array. If we pass a string to the sub, it is always in the
# first element. If we pass an array the elements are the same as they were in the original array.
}

This example will print "Print This String\n";

Lesson 7a - Regular Expressions

Home | Lessons | Get Perl | Resources