|
|||||||||||
|
Perl Program Flow ControlWhat is flow control? Flow control is telling a program to do certain things under certain conditions. For example, in our introduction we described a process for making breakfast; steps two and three were repeated until the right number of eggs were added to a mixing bowl. Let's look at a program that illustrates this part of the process:
#!/usr/bin/perl -w
use strict;
my $eggs_in_carton = 12;
my $eggs_in_bowl = 0;
my $eggs_to_make = 3;
my $egg_counter;
for ($egg_counter = 0; $egg_counter < $eggs_to_make; $egg_counter++)
{
$eggs_in_carton = $eggs_in_carton - 1;
$eggs_in_bowl = $eggs_in_bowl + 1;
}
print "There are $eggs_in_carton eggs left in the carton.\n";
print "There are $eggs_in_bowl in the mixing bowl.\n";
pparadis@ubuntu-devel:~$ ./example.pl There are 9 eggs left in the carton. There are 3 in the mixing bowl. Before we go any further, it's worth pointing out that we've also shown how you can initialize a variable when you declare it; in many cases this makes your code more concise and easier to read. Getting back to our learning objective, this code demonstrates use of a for loop to effect flow control. We start with twelve eggs in the carton, and use the scalar "egg_counter" to keep track of how many times we've gone around our loop. Each time the loop executes, Perl checks whether the value of $egg_counter is still less than the value held in $eggs_to_make. Keeping with personal tradition, I always use zero-based loops for counting; it makes it easier to deal with cases where I'm using the counter variable as the index into an array. The last part of the "for" line increments the value of $egg_counter by one each time the loop executes ("++" is shorthand for "add one to this value"). So, once the value of $egg_counter reaches three the loop stops executing and control is transferred to the next statement in our program. The number of eggs left in the carton and in mixing bowl is displayed on the screen. Using if Conditional Statements Let's add some code to our program to handle a case where we don't have enough eggs to serve the desired amount for breakfast:
#!/usr/bin/perl -w
use strict;
my $eggs_in_carton = 12;
my $eggs_in_bowl = 0;
my $eggs_to_make = 13;
my $egg_counter;
if ($eggs_to_make > $eggs_in_carton)
{
print "You don't have enough eggs!\n";
exit;
}
for ($egg_counter = 0; $egg_counter < $eggs_to_make; $egg_counter++)
{
$eggs_in_carton = $eggs_in_carton - 1;
$eggs_in_bowl = $eggs_in_bowl + 1;
}
print "There are $eggs_in_carton eggs left in the carton.\n";
print "There are $eggs_in_bowl in the mixing bowl.\n";
pparadis@ubuntu-devel:~$ ./example.pl You don't have enough eggs! We're trying to make thirteen eggs, but we only have twelve on hand. The if conditional statement does a sanity check on our breakfast wishes before starting to break eggs. If we ask for more than the number of eggs in the carton, the program informs us of the problem and quits via the "exit" command. If you wanted to congratulate the program user on having enough eggs before proceeding to add them to the bowl, you could change the code to look like this:
if ($eggs_to_make > $eggs_in_carton)
{
print "You don't have enough eggs!\n";
exit;
}
else { print "You've got enough eggs, let's make them!\n"; }
The "else" statement that immediately follows the "if" check tells the program what to do if the condition being testing by "if" isn't met. If we have enough eggs, the program emits a message to this effect and continues making our breakfast. Notice the lack of line breaks and indentation in the "else" statement; this demonstrates the fact that Perl doesn't care about whitespace (tabs, spaces, line breaks, etc) in most cases. Program formatting is largely left up to the programmer, allowing you to pick a style that suits you. What if we need to check for more than one condition in a program? The if statement allows for this:
#!/usr/bin/perl -w
use strict;
my $gallons_of_gas = 10;
my $got_keys = 1;
if (($gallons_of_gas >= 1) && ($got_keys == 1))
{
print "We can drive.\n";
}
else
{
if ($gallons_of_gas < 1)
{
print "We need more gas.\n";
}
if ($got_keys != 1)
{
print "We need the car keys.\n";
}
exit;
}
pparadis@ubuntu-devel:~$ ./example.pl We can drive. This code does a couple of checks to make sure we're ready to drive a car; we need to have gasoline and the car keys. The if statement is broken into two parts, contained by sets of parentheses. The "&&" operator means "and" in Perl; if both conditions evaluate as true then the program tells us we can drive. If either statement evaluates as false, the else statement is run and we're informed of the problem(s). The operator ">=" means "greather than or equal to", the operator "==" means "equals" (as opposed to "=", which assigns a value), and "!=" means "not equal to". Assigning any value other than "1" to $got_keys will trigger the car keys error message. Traditionally, programmers use "1" for "true" and "0" for "false" when setting or testing true/false conditions, but these are arbitrary values that can be set as you please in your programs. Using while and unless Conditional Statements Have a look at this example for more conditional statement use:
#!/usr/bin/perl -w
use strict;
my $pizzas = 3;
my $empty_boxes = 0;
while ($pizzas > 0)
{
$pizzas = $pizzas - 1;
$empty_boxes = $empty_boxes + 1;
print "We ate a pizza.\n";
}
unless ($empty_boxes == 0)
{
print "Take out the garbage. We have $empty_boxes boxes.\n";
}
pparadis@ubuntu-devel:~$ ./example.pl We ate a pizza. We ate a pizza. We ate a pizza. Take out the garbage. We have 3 boxes. This program describes a pizza party; we start with three pizzas and no empty boxes. As the party progresses, our while loop checks to see if there's any pizza left. Once it's all gone, the program checks to see if there are any empty boxes (of course there are), and reminds us to clean up after ourselves. Unless you're living under a bridge, that's probably a good idea. If you are living under a bridge, and have somehow managed to establish reliable Internet access, I commend you on your resourcefulness. The next chapter introduces file handling, allowing you to read and write information stored on your hard disk. Continue: File Input/Output Table of Contents
|
||||||||||
| All contents Copyright © 2007-2009 ClassHelper.org, except where content is indicated to be copyrighted by a contributing author. | |||||||||||