control-flow intermediate

How to use loop labels with next and last in Perl?

Question

How to use loop labels with next and last in Perl?

Using Loop Labels with next and last in Perl

Perl offers great flexibility in its loop control constructs, and one useful feature is the ability to label loops. Loop labels let you precisely control which loop next (skip to the next iteration) or last (exit immediately) applies to, especially useful when dealing with nested loops.

By default, next and last affect the innermost loop they are within. However, when loops are nested, you might want to skip or break an outer loop instead. This is where loop labels come into play.

What are loop labels?

  • A loop label is an identifier followed by a colon (NAME:) placed before the loop statement.
  • This label names the loop so that control statements like last, next, and redo can reference it explicitly.

How to use them

LABEL: while (<condition>) {
    # loop body
    next LABEL if <condition>;  # skip current iteration of LABEL loop
    last LABEL if <condition>;  # exit LABEL loop immediately
}

Example with nested loops

Here we demonstrate two loops. We will use next and last to control flow in specific loops via labels:

use strict;
use warnings;

OUTER: for my $i (1 .. 3) {
    INNER: for my $j (1 .. 5) {
        print "i=$i, j=$j\n";
        
        if ($j == 3) {
            print "  next OUTER (skip to next i)\n";
            next OUTER;    # skips remaining inner loop, goes to next outer iteration
        }
        
        if ($i == 2 && $j == 2) {
            print "  last OUTER (exit both loops)\n";
            last OUTER;   # breaks out of both loops entirely
        }
    }
}

print "Loops finished.\n";

Explanation

  • OUTER is a label for the outer for loop, and INNER for the inner one.
  • next OUTER skips the rest of the inner loop and proceeds to the next iteration of the outer loop.
  • last OUTER completely exits the outer loop, effectively ending both loops.

Key Points & Gotchas

  • Labels are arbitrary identifiers: You can choose any valid Perl identifier for your labels, just avoid reserved keywords.
  • Mandatory for nested loop control: Without labels, next and last only apply to the innermost loop.
  • Context matters: last exits the loop immediately, while next skips the rest of the current iteration but continues the loop.
  • Perl versions: Loop labels with last and next have been supported since early Perl 5 versions.
  • Common pitfall: Forgetting to label your loops when you want to control a specific outer loop leads to undesired behavior, since unmatched next/last ignore the labels.

Summary

Using loop labels with next and last allows fine-grained control over complex nested loops. It is a handy feature when you want to jump or exit certain loops without resorting to convoluted logic. Just prefix your loops with labels and apply last LABEL or next LABEL for clean, readable control flow.

Verified Code

Executed in a sandbox to capture real output. • v5.34.1 • 4ms

Tip: edit code and use “Run (Browser)”. Server runs always execute the published, verified snippet.
STDOUT
(empty)
STDERR
(empty)

Was this helpful?

Related Questions