9.5 Expression ModifiersAs Yet Another Way to indicate "if this, then that," Perl allows you to tag an if modifier onto an expression that is a standalone statement, like this:
In this case, if ( except that you don't need the extra punctuation, the statement reads backwards, and the expression must be a simple expression (not a block of statements). Many times, however, this inverted description turns out to be the most natural way to state the problem. For example, here's how you can exit from a loop when a certain condition arises: LINE: while (<STDIN>) { last LINE if /^From: /; } See how much easier that is to write? And you can even read it in a normal English way: "last line if it begins with From." Other parallel forms include the following:
All of these forms evaluate For example, here's how to find the first power of two greater than a given number: chomp($n = <STDIN>); $i = 1; # initial guess $i *= 2 until $i > $n; # iterate until we find it Once again, we gain some clarity and reduce the clutter.
These forms don't nest: you can't say |
|