Richard's Perl Step 03
Controlling the Program's Flow



- I used "Sams Teach Yourself Perl in 24 Hours" - 3rd Edition as a reference for this page
- Run in windows DOS prompt
perl thisprogram.pl

perl c:\......\thisprogram.pl


Examples following Perl 24 hours book, Hour 3:

eg003.pl (text only) - if numeric

eg004.pl (text only) - if nonnumeric, and, or

eg005.pl (text only) - while, for loops

 

The if Statement

$r=<STDIN>;
chomp $r;
if ($r == 10) {
print '$r is 10', "\n";
} else {
print '$r is something other than 10...', "\n";
$r=10;
print '$r has been set to 10', "\n"; }

notice that to assign a value to $r, I used the assignment operator, =. To test the value of $r, I used the numeric equality test operator, ==.

if (expression1)     # If expression1 is true ...
     block1           # ...run this block of code.

elsif (expression2) # Otherwise, if expression2 is true... block2 # ...Run this block of code. else block3 # If neither expression was true, run this.

The Other Relational Operators

 

Operator

Example

Explanation

 

==

$x == $y

True if $x equals $y

 

>

$x > $y

True if $x is greater than $y

 

<

$x < $y

True if $x is less than $y

 

>=

$x >= $y

True if $x is greater than or equal to $y

 

<=

$x <= $y

True if $x is less than or equal to $y

 

!=

$x != $y

True if $x is not equal to $y

If you want to test nonnumeric values, you can use another set of Perl operators

 

Operator

Example

Explanation

 

Eq

$s eq $t

True if $s is equal to $t

 

Gt

$s gt $t

True if $s is greater than $t

 

Lt

$s lt $t

True if $s is less than $t

 

Ge

$s ge $t

True if $s is greater than or equal to $t

 

Le

$s le $t

True if $s is less than or equal to $t

 

Ne

$s ne $t

True if $s is not equal to $t

These operators decide "greater than" and "less than" by examining each character left to right and comparing them in ASCII order. This means that strings sort in ascending order: most punctuation first, and then numbers, uppercase, and finally lowercase.


What Truth Means to Perl


Logical Operators

 

Operator

Alternative Name

Example

Analysis

 

&&

and

$s && $t

True only if $s and $t are true

 

 

 

$q and $p

True only if $q and $p are true

 

||

or

$a || $b

True if $a is true or $b is true

 

 

 

$c or $d

True if $c is true or $d is true

 

!

not

 

 


Looping

Looping with while

while (expression) block

$counter=0;
while ($counter < 10 ) {
     print "Still counting...$counter\n";
     $counter++;
}

Looping with for

for ( initialization; test; increment ) block

for( $a=0; $a<10; $a=$a+2 ) {

print "a is now $a\n"; }
$i=10;           # initialization
for( ; $i>-1; ) {
     print "$i..";
     $i--;          # actually, a decrement.
}
print "Blast off!\n";


Other Flow Control Tools

The last statement causes the while loop to exit when the value of $i is 5, instead of normally when the while test is false. When you have multiple nested loop statements, last exits the loop currently running.}

while($i<15) {       
     last if ($i==5);        
     $i++;    
}
for($i=0; $i<100; $i++) {        
     next if (not $i % 2);        
     print "An odd number=$i\n";    
}   

This loop prints all the even numbers from 0 to 98. The next statement causes the loop to go through its next iteration if $i is not even; the $i % 2 expression is the remainder of $i divided by 2. In this case, the print statement is skipped.

Labels

Perl allows blocks and some loop statements (for, while) to be labeled. That is, you can place an identifier in front of the block or statement:

OUTER: for($i=0; $i<100; $i++) {
     for($j=0; $j<100; $j++) {
          if ($i * $j == 140) {
              print "The product of $i and $j is 140\n";
              last OUTER;
          }
    }
}

Now the last statement can specify which loop it wants to exit—in this case, the OUTER loop. This snippet prints only the first pair of factors of 140 that it finds.


Leaving Perl

The exit statement is the ultimate flow-control tool. When Perl encounters an exit statement, the program stops executing, and an exit status is returned by Perl to the operating system. This exit status is usually used to indicate successful completion of the program.

if ($user_response eq 'quit') {
    print "Good Bye!\n";
    exit 0;        # Exit with a status of 0.
}

The exit statement has some side effects that are important to your operating system. When an exit is performed, any open files are closed, file locks are released, memory allocated by Perl is released to the system, and the Perl interpreter performs a clean shutdown.