9.6 && and || as Control StructuresThese look like punctuation characters or parts of expressions. Can they really be considered control structures? Well, in Perl-think, almost anything is possible, so let's see what we're talking about here. Often, you run across "if this, then that." We've previously seen these two forms: if ( Here's a third (and believe it or not, there are still others):
Why does this work? Isn't that the logical-and operator? Check out what happens when
And in fact, this is what Perl does. Perl evaluates
Likewise, the logical-or works like the unless ( with:
If you're familiar with using these operators in shell programming to control conditional execution commands, you'll see that they work similarly in Perl. Which one should you use? It depends on your mood, sometimes, or how big each of the expression parts are, or whether you need to parenthesize the expressions because of precedence conflicts. Look at other people's programs, and see what they do. You'll probably see a little of each. Larry suggests that you put the most important part of the expression first, so that it stands out. |
|