Loop control

You can put a label on a loop to give it a name. The loop’s label identifies the loop for the loop-control commands next, last, and redo:

LINE: while (<SCRIPT>) {
    print;
    next LINE if /^#/;      # Discard comments
    }

The syntax for the loop-control commands is:

last label
next label
redo label

If the label is omitted, the loop-control command refers to the innermost enclosing loop.

The last command is like the break statement in C (as used in loops); it immediately exits the loop in question. The continue block, if any, is not executed.

The next command is like the continue statement in C; it skips the rest of the current iteration and starts the next iteration of the loop. If there is a continue block on the loop, it is always executed just before the conditional is reevaluated.

The redo command restarts the loop block without evaluating the conditional again. The continue block, if any, is not executed.

Get Perl in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.