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 statement work? Isn't that the logical-and operator? Check out what happens when
And in fact, Perl does just that. Perl evaluates
Likewise, the logical unless ( with
Finally, the C-like ternary operator:
evaluates to if ( but you could have eliminated all of that punctuation. For example, you could write: ($a < 10) ? ($b = $a) : ($a = $b); Which one should you use? Your choice depends on your mood, sometimes, or on how big each of the expression parts are, or on 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. |
|