Going Deeper

As with the previous lessons, I still haven't told you everything you might want to know about conditionals and loops. In this section, I'll summarize some of these other features. Feel free to explore these features of the Perl language on your own.

Conditional and Loop Modifiers

The conditionals and loops you learned about in today's lesson are all complex statements—they operate on blocks of other statements, and don't require a semicolon at the end of the line. However, Perl also includes a set of modifiers to simple statements that can be used to form conditional and loop-like expressions. Sometimes these modifiers can provide shorter versions of simple conditionals and loops; other times they can help you express the logic of a statement you're trying to create in Perl that doesn't quite fit into the traditional conditionals or loops.

Each of the modifiers follows a simple statement, just before the semicolon. There are four of them, which mimic the more complex versions: if, unless, while, and until. Here are some examples:

print "$value" if ($value < 10);
$z = $x / $y if ($y > 0);
$i++ while ($i < 10);
print $value until ($value++ > $maxvalue)

In the conditional case, the front part of the statement will only execute if the test is true (or, with unless, if it's false). With the while and until loops, the statement will repeat until the test is false (or true, in the case of until).

Note that loops created with while and until modifiers are not the same as regular while and until loops; you cannot control them with loop control statements like next or last, nor can you label them.

The do loops you learned about earlier in this lesson are actually forms of loop modifiers. do is actually a function that executes a block of code, and you can use while and until to repeat that block of code some number of times based on a test (you'll learn more about do on Day 13). This is why you cannot use loop controls in do loops either.

Using continue blocks

The continue block is an optional block of statements after a loop that gets executed if the block finished executing, or if the loop was interrupted using next. The continue block is not executed if the loop was interrupted using last or redo. After the continue block is executed, Perl continues with the next iteration of the loop.

You might use a continue block to collect code that would otherwise be repeated for several different loop-exit situations, to change variables in those situations, or do some kind of cleaning up from an error that resulted in the loop exiting in the first place. It looks like this (where the while loop here could be a for or a foreach; any loop will do):

while ( test ) {
   # statements
   if (anothertest) {
      # error!
      next;  # skip down to continue
   }
   # more statements
} continue {
   # cleanup code from error
   # after this is done, go back to top of loop
}
						

Constructing switch or case Statements

Perl, remarkably, does not have syntax for an explicit switch statement. A switch, sometimes known as a case depending on your favorite language, is a construct that enables you to test a value and get a result in a much more compact, often more efficient, and easier-to-read manner than a whole lot of nested ifs. However, with labeled blocks, do loops, and logical expressions, you can build something that looks at least a little bit like a switch. Add pattern matching to it, and you get a Perl-like switch that works pretty well.

For example, here's a simple switch-like thing in Perl:

SWITCH: {
   $a eq "one" && do {
                       $a = 1;
                       last SWITCH;
                     };
   $a eq "two" && do {
                       $a = 2;
                       last SWITCH;
                     };
   $a eq "three" && do {
                       $a = 3;
                       last SWITCH;
                       };
# and so on
}

On Day 9, you'll learn a way to make that even shorter with patterns.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset