Loop Statements
All loop statements have an optional
LABEL in their formal syntax. (You can put
a label on any statement, but it has a special meaning to a loop.) If
present, the label consists of an identifier followed by a colon. It's
customary to make the label uppercase to avoid potential confusion
with reserved words, and so it stands out better. And although Perl
won't get confused if you use a label that already has a meaning like
if or open, your readers might.
while and until Statements
The while statement repeatedly
executes the block as long as EXPR is
true. If the word while is replaced by the word
until, the sense of the test is reversed; that
is, it executes the block only as long as
EXPR remains false. The conditional is
still tested before the first iteration, though.
The while or
until statement can have an optional extra block:
the continue block. This block is executed every
time the block is continued, either by falling off the end of the
first block or by an explicit next (a
loop-control operator that goes to the next iteration). The
continue block is not heavily used in practice,
but it's in here so we can define the for loop
rigorously in the next section.
Unlike the foreach loop we'll see in a
moment, a while loop never implicitly localizes
any variables in its test condition. This can have "interesting"
consequences when while loops use globals for
loop variables. In particular, see Section 2.11.2 in Chapter 2 for how implicit assignment
to the global
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