Simple Statements
A simple statement is an expression evaluated for its side effects. Every simple statement must end in a semicolon, unless it is the final statement in a block. In that case, the semicolon is optional—Perl knows that you must be done with the statement since you’ve finished the block. But put the semicolon in anyway if it’s at the end of a multiline block, because you might eventually add another line.
Even though operators like eval
{}, do {}, and sub {} all look like compound statements,
they really aren’t. True, they allow multiple statements on the
inside, but that doesn’t count. From the outside, those operators are
just terms in an expression, and thus they need an explicit semicolon
if used as the last item in a statement.
Any simple statement may optionally be followed by a single modifier, just before the terminating semicolon (or block ending). The possible modifiers are:
if EXPR unless EXPR while EXPR until EXPR for LIST when EXPR
The if and unless modifiers work pretty much as they do in English:
$trash–>take("out") if $you_love_me;
shutup() unless $you_want_me_to_leave;The while and until modifiers evaluate repeatedly. As you might expect, a
while modifier keeps executing the
expression as long as its expression remains true, and an until modifier keeps executing only as long
as it remains false:
$expression++ while –e "$file$expression";
kiss("me") until $I_die;The for modifier (also spelled foreach if you’re trying to wear out your keyboard) evaluates ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access