Introductory Perl - Control Structures 
3/5 

Conditionals allow you to execute code only if certain conditions are met. Some structures apply by default to only one line of code; if you want to apply them to more than one line of code, or for those that require code blocks, simply enclose the lines in {}s.
In Perl, most things (defined variables, numbers other than 0, etc) are positive by default. Empty strings, the number 0, and undefined values are considered false. Function calls evaluate to whatever value they return.


from O'Reilly's Programming Perl:

...to have control, you have to be able to decide things, and to decide things, you have to know the difference between what's true and what's false.

Here are links to some especially important components of your briefing:
Starting Perl
Variables
Control Structures
Subroutines
More...

Q
U
I
C
K

L
I
N
K
S
If

The simplest of all conditional statements is if. It executes the statement block following it only if the conditional test is true. The test does not need to be a variable; making it a true constant will make the statement execute no matter what (this can be useful for debugging, but otherwise it's just a waste). The syntax looks like this:

Code

ResultDownload
#!/usr/local/bin/perl
$v = 1;
$f = 0;
if ($v) {
print "V is true\n";
}
if ($f) {
print "f is true\n";
}
v is true


if.pl

Other tests besides the variable's state are possible. The =, >, and < symbols work just like in math class, and <= and >= modify them slightly to include the value being compared. ! can be used to modify any of the preceeding symbols to reverse the logic, so for instance != means not equal to and !>= is the same as <.

Unless

Unless is essentially identical to !if, and for that reason a lot of people choose not to use it. But in accordance with its philosophy of unconstraint, Perl allows you to code as you like. Replacing the if statements in the previous examples produces (as expected) the opposite result:

Code

ResultDownload
#!/usr/local/bin/perl
$v = 1;
$f = 0;
unless ($v) {
print "V is true\n";
}
unless ($f) {
print "f is true\n";
}
f is true


unless.pl

The fact that the program outputs what is essentially a false statement illustrates the importance of keeping track of exactly what logical operators are doing.

While and Until

While executes its block until its condition returns false. It's a very simple way to make sure something continues to happen until certain other things do. The syntax looks like this:

while (condition)
{ some code }

Until is the logical opposite of while and executes its block until the condition is true.

For

A for loop (like in other languages) executes its block until its condition is met. Unlike other structures, though, it has a built-in way to initialize variables to a certain value, and a bit of code to run each time before it repeats. A typical for loop looks like this:

for (initialization ; condition ; incrementation)
{ some code }

The initialization section is normally used to set a variable to some known value (like zero) and is run as soon as the interpreter reaches it, before anything else. It is run only once.

Next, the interpreter checks the condition which works just like an if statement. The for loop will execute its code block until so long as this statement remains true.

Each time the interpreter reaches the end of the code block, it executes the incrementation section, which is normally used to add something to (increment) a variable so that at some point the condition forces the loop to end.

An in-depth (including assembly) explanation of exactly how this works is available here.