| | |||||||
| |||||||
| |||||||
|
|
|
|
|
|
|
Expression Modifiers for Perl Control Flow Constructs(Page 1 of 2 ) In this fifth part of a nine-part article series on Perl control structures, you'll learn about expression modifiers, looping statements, and more. This article is excerpted from chapter three of the book Beginning Perl, Second Edition, written by James Lee (Apress; ISBN: 159059391X). The unless Statement There’s another way of saying if (not $a). As always in Perl, there’s more than one way to do it.2 Some people prefer to think “If this is not true, then{ ... },” but other people think “Unless this is true, then{ ... }. ” Perl caters to both sets of thought patterns, and we could just as easily have written this: unless ($a) { The psychology is different, but the effect is the same. We’ll see later how Perl provides a few alternatives for these control structures to help them more effectively fit the way you think. Expression Modifiers When we’re talking in English, it’s quite normal for us to say
Similarly, it’s also quite natural to reverse the two phrases, saying
In Perl-speak, we can take thisifstatement: if ($number == 0) { and rewrite it using expression modifiers as follows: die "can't divide by 0" if $number == 0; Notice how the syntax here is slightly different, it’s action ifcondition. There is no need for parentheses around the condition, and there are no curly braces around the action. Indeed, the indentation isn’t part of the syntax, so we could even put the whole statement on one line. Only a single statement will be covered by the condition. This form of theifstatement is called an expression modifier. We can turnunlessinto an expression modifier too, so, instead of this: if (not $name) { you may find it more natural to write this: die "\$name has a false value" unless $name; Using Short-Circuited Evaluation There is yet another way to do something if a condition is true. By using the fact that Perl stops processing a logical operator when it knows the answer, we can create a sort of unlessconditional: $name or die "\$name has a false value"; How does this work? Well, it’s reliant on the fact that Perl uses short-circuited, or lazy, evaluation to give a logical operator its value. If we have the statementX or Y, then ifXis true, it doesn’t matter whatY is, so Perl doesn’t look at it. IfXisn’t true, Perl has to look atYto see whether or not that’s true. So if$namehas a true value, then thedie()function will not be executed. Instead, it will do nothing and continue on executing the next statement. This form of conditional is most often used when checking that something we did succeeded or returned a true value. We will see it often when we’re handling files. To create a positiveifconditional this way, useand instead ofor. For example, to add one to a counter if a test is successful, you may say $success and $counter++; If you’ll recall,andstatements are reliant on both substatements being true. So, if$successis not true, Perl won’t bother evaluating$counter++and upping its value by 1. If$successwas true, then it would. More Programming Basics Articles |
| |
| |