Выбрать главу

 statement(s)

}

The start condition is set at the beginning of the loop. Each time the loop is executed, the increment function is performed until the end condition is achieved. This looks much like the traditional for/next loop. The following code is an example of a for loop:

for ($i=1; $i<=10; $i++) {

 print "$i\n"

}

foreach

The foreach construct performs a statement block for each element in a list or array:

@names = ("alpha","bravo","charlie");

foreach $name (@names) {

 print "$name sounding off!\n";

}

The loop variable ($name in the example) is not merely set to the value of the array elements; it is aliased to that element. That means if you modify the loop variable, you're actually modifying the array. If no loop array is specified, the Perl default variable $_ may be used:

@names = ("alpha","bravo","charlie");

foreach (@names) {

 print "$_ sounding off!\n";

}

This syntax can be very convenient, but it can also lead to unreadable code. Give a thought to the poor person who'll be maintaining your code. (It will probably be you.)

NOTE

foreach is frequently abbreviated as for.

while

while performs a block of statements as long as a particular condition is true:

while ($x<10) {

 print "$x\n";

 $x++;

}

Remember that the condition can be anything that returns a true or false value. For example, it could be a function calclass="underline"

while ( InvalidPassword($user, $password) ) {

 print "You've entered an invalid password. Please try again.\n";

 $password = GetPassword;

}

until

until is the exact opposite of the while statement. It performs a block of statements as long as a particular condition is false — or, rather, until it becomes true:

until (ValidPassword($user, $password)) {

 print "You've entered an invalid password. Please try again.\n";

 $password = GetPassword;

}

last and next

You can force Perl to end a loop early by using a last statement. last is similar to the C break command—the loop is exited. If you decide you need to skip the remaining contents of a loop without ending the loop itself, you can use next, which is similar to the C continue command. Unfortunately, these statements don't work with do ... while.

On the other hand, you can use redo to jump to a loop (marked by a label) or inside the loop where called:

$a = 100; while (1) {

 print "start\n";

 TEST: {

  if (($a = $a / 2) > 2) {

   print "$a\n";

    if (--$a < 2) {

    exit;

   }

   redo TEST;

  }

 }

}

In this simple example, the variable $a is repeatedly manipulated and tested in an endless loop. The word "start" is printed only once.

do ... while and do ... until

The while and until loops evaluate the conditional first. You change the behavior by applying a do block before the conditional. With the do block, the condition is evaluated last, which results in the contents of the block always executing at least once (even if the condition is false). This is similar to the C language do ... while (conditional) statement.

Regular Expressions

Perl's greatest strength is in text and file manipulation, which it accomplishes by using the regular expression (regex) library. Regexes, which are quite different from the wildcard handling and filename expansion capabilities of the shell, allow complicated pattern matching and replacement to be done efficiently and easily. For example, the following line of code replaces every occurrence of the string bob or the string mary with fred in a line of text:

$string =~ s/bob|mary/fred/gi;

Without going into too many of the details, Table 25.7 explains what the preceding line says.

TABLE 25.7 Explanation of $string =~ s/bob|mary/fred/gi;

Element Explanation
$string =~ Performs this pattern match on the text found in the variable called $string.
s Substitutes one text string for another.
/ Begins the text to be matched.
bob|mary Matches the text bob or mary. You should remember that it is looking for the text mary, not the word mary; that is, it will also match the text mary in the word maryland.
/ Ends text to be matched; begins text to replace it.
fred Replaces anything that was matched with the text fred.
/ Ends replace text.
g Does this substitution globally; that is, replaces the match text wherever in the string you match it (and any number of times).
i Make the search text case insensitive. It matches bob, Bob, or bOB.
; Indicates the end of the line of code