EGTDC Perl Course 2004
Dan Swan: Flow Control ([email protected])
Statement Blocks
●A statement block is a way of executing statements, in sequence.
●They look like:
● {
● first_statement;
● second_statement;
● third_statement;
● }
●Why do we have them?
●Very useful for blocking off bits of code so that one is executed if one condition is true but another is executed if it is not.
●There are various ways of making decisions in Perl.
●
A note on equality
• When we use control structures, we generally compare one thing to another.
• What we are looking for in generalised terms is "do X if A=B" or "do Y if A=C".
• When comparing scalars you can compare them in a numerical context or a string context.
• Equals:
– $integer == 1
– $string eq "perl"
• Not equals:
– $integer != 1
IF/ELSE
• A control expression that IF the condition is true, one statement block is executed, ELSE a different statement block is executed (ifelse.pl).
•
– if (control_expression is TRUE) {
• do this;
• and this;
• } else {
• do that;
• and that;
• }
#ifelse.pl use warnings;
use strict;
if (1 == 2) {
print "1 is equal to 2!\n";
} else {
print "1 is not equal to 2, silly\n";}
exit();
ELSIF
• if/else is great for yes/no decisions. If you want to test multiple statements you can combine else and if to make 'elsif' (elsif.pl).
–
– if (condition 1 is TRUE) {
• do this;
• } elsif (condition 2 is TRUE) {
• do that;
• } elsif (condition 3 is TRUE) {
• do the other;
• } else { #all tests are failed
• do whatever;
• }
#elsif.pl
use warnings;
use strict;
if (1 == 2) {
print "1 equals 2!\n";
} elsif (1 == 3) {
print "1 equals 3!\n";
} elsif (1 ==1) {
print "1 equals 1!\n";
} else {
print "Something has gone tragically wrong\n";
}
exit();
WHILE
• Lets say you want to do a series of actions whilst a certain condition is true (while.pl):
•
•
– while (expression is true) {
• do this;
• do that;
• do the other; #until no longer true
• }
#while.pl
use warnings;
use strict;
my $number;
$number = 1;
#as an aside the above two lines could be written
#"my $number = 1;"
while ($number == 1) {
print "Scalar \$number is 1\n";
$number++;
}
print "Scalar \$number is no longer 1 and as it is
$number the loop has terminated!\n";
FOREACH
• Takes a list of values and assigns them to a scalar variable, which executes a block of code (foreach.pl).
●
● foreach $element (@list) {
● do this;
● do that;
● do the_other; #until no more $element's
● }
#foreach.pl use warnings;
use strict;
my ($element, @list);
@list = ("one", "two", "three", "four",
"five", "six", "seven", "moo");
foreach $element (@list) { print "$element\n";
}
exit();
FOREACH(2)
• There's a few things missing from this code snippet compared to the previous one (foreach2.pl).
• No specification of $element this time. And yet it still works! Why?
•
● foreach (@list) {
● do this;
● do that;
● do the_other;
● }
#foreach2.pl use warnings;
use strict;
my (@list);
@list = ("one", "two", "three", "four",
"five", "six", "seven", "moo");
foreach (@list) { print;
print "\n";
}
exit();
FOREACH(3)
• There is an implied scalar on the previous slide $_
• The $_ is a special scalar variable almost like a scratchpad its a container for information (foreach3.pl). Notice it works both for the foreach AND the print statement.
• Perl knows that if you use foreach (@list) that it is going to assign each element to a scalar so it will use $_ by default.
● foreach $_ (@list) {
● do this;
● do that;
● do the_other; #until no more $_'s
● }
#foreach3.pl use warnings;
use strict;
my (@list);
@list = ("one", "two", "three", "four",
"five", "six", "seven", "moo");
foreach $_ (@list) { print "$_\n";
}
exit();
FOR
• The statement people remember from BASIC (or C!)
• An initial expression is set up ($init_val), a test expression is then set up which is tested at each iteration ($init_val < 10). Finally the initial
expression is changed with each loop iteration ($init_val++).
•
– for ($init_val = 0; $init_val < 10; $init_val++) {
• print "$init_val\n";
• }
#for.pl
use warnings;
use strict;
my ($init_val);
for ($init_val = 1; $init_val < 10; $init_val++) {
print "The scalar \$init_val has value = $init_val\n";
}
print "\nThe loop has ended!\n";
exit();
A few asides other control structures
• unless/else like if/else but unless (false) rather than if (true).
• do/while and do/until "does" a statement block "while" a condition is evaluated or "does" a statement block "until" expression is evaluated.
• last allows you to get out of a loop early e.g. instead of loop finishing when loop conditions are met a loop can end when
conditions internal to the loop are met. See also "next" "redo" and read up on "labelled blocks" for more info.
Licence
• This work is licensed under the Creative Commons Attribution
NonCommercialShareAlike License. To view a copy of this license, visit http://creativecommons.org/licenses/byncsa/1.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
94305, USA.