Perl Control Constructs

Home

In Perl, these include loops and branches, but no multi-branch.

Curly brackets should be used around control construct statement blocks, even when there is only one statement.

Loops

# Example of looping in Perl with a while loop

$input = ;
while $input ne "" {
print ($input);
}

# Example of looping in Perl with a for loop

for ($i = 0; $i < @myarray ; $i++) {
print ($myarray[$i]\n");
}

Branches

# Example of branching in Perl with an if-else statement

if ($myvariable eq "mine all mine") {
print "You're not having it!";
}
else {
print "OK, it's yours!";
}

Back to top     Home     Serious Stuff

© 2001. Copyright Sue Nethercott.