Control flow

break#

Break out of a given block.

break terminates the enclosing given block immediately and transfers control to the statement following it — the given analogue of what last does for loops. It takes no arguments, returns no value, and applies only to the innermost given; it cannot target an outer block by label.

Synopsis#

break

Availability — read this first#

break is a keyword of the switch feature, together with given, when, default, and (inside a when) continue. In Perl 5.42 the switch feature is disabled by default and is not part of any current feature bundle: use v5.36 through use v5.42 do not turn it on. The feature still exists and can be used, but only under explicit opt-in:

use feature 'switch';
no warnings 'experimental::smartmatch';

Without that opt-in, the bareword break is parsed as an ordinary subroutine call and fails at compile time with Bareword "break" not allowed while "strict subs" in use, or at runtime with Undefined subroutine &main::break called. The fully-qualified form CORE::break works from any scope without enabling the feature and is how you invoke it in code that otherwise keeps switch off.

given/when rely on smartmatch (~~), which is itself experimental and warns on every use unless experimental::smartmatch warnings are suppressed. New code should not reach for given/when/break; see Replacements below.

Behaviour#

Inside a given block, break unwinds to the statement immediately after the block. It is equivalent to what falls through the bottom of a when branch implicitly — when clauses break at the end of their body unless the body ends in continue, so an explicit break is mostly used for early exit from a long when body:

use feature 'switch';
no warnings 'experimental::smartmatch';

given ($cmd) {
    when ('quit') {
        cleanup();
        break if $fast_exit;        # skip the logging below
        log_exit();
    }
    when ('help') { show_help() }
    default       { warn "unknown: $cmd" }
}

Outside a given block, break raises Can't "break" outside a given block at runtime.

Replacements#

For new code, prefer one of these forms. They are plain Perl, need no feature opt-in, and have no deprecation cloud over them:

  • if / elsif / else chain — direct replacement for most given/when usage:

    if    ($cmd eq 'quit') { cleanup(); log_exit() }
    elsif ($cmd eq 'help') { show_help() }
    else                   { warn "unknown: $cmd" }
    
  • Dispatch table — a hash of coderefs keyed by the input, with a default branch. Scales better than a long elsif ladder and makes the set of handled cases data rather than control flow:

    my %dispatch = (
        quit => sub { cleanup(); log_exit() },
        help => \&show_help,
    );
    ($dispatch{$cmd} // sub { warn "unknown: $cmd" })->();
    
  • Labelled loop with last — when you specifically want the “exit this block early” shape that break provided, wrap the body in a bare block and last out of it:

    CMD: {
        if ($cmd eq 'quit') { cleanup(); last CMD if $fast_exit; log_exit(); last CMD }
        if ($cmd eq 'help') { show_help(); last CMD }
        warn "unknown: $cmd";
    }
    

Examples#

Explicit feature opt-in, early-exit from a when body:

use feature 'switch';
no warnings 'experimental::smartmatch';

given ($n) {
    when ($_ < 0) { warn "negative"; break }   # skip the log below
    when (0)      { warn "zero" }
    default       { warn "positive: $n" }
}
# control resumes here after break

Using CORE::break without enabling the feature globally:

sub classify {
    my ($n) = @_;
    given ($n) {
        when ($_ < 0) { return "neg"; CORE::break }
        when (0)      { return "zero" }
        default       { return "pos" }
    }
}

Edge cases#

  • No argument form. break takes nothing. break LABEL is a syntax error — unlike last/next, break has no way to target an outer block.

  • Outside given. Calling break (or CORE::break) when no given block is on the call stack raises Can't "break" outside a given block.

  • return wins. Inside a subroutine, return unwinds the whole sub regardless of any enclosing given. break only unwinds to the end of the given; control continues in the surrounding sub.

  • Implicit break at end of when. A when clause’s body ends with an implicit break unless the body is continue, so an explicit break at the end of a when body is redundant. continue suppresses the implicit break and lets control fall through to the next when.

  • Not a loop keyword. break does not interact with for, while, foreach, do, or bare blocks. For those, use last.

  • Experimental warnings. Running given/when code without no warnings 'experimental::smartmatch' prints the warning once per smartmatch site, which in a busy given block can be every iteration.

Differences from upstream#

Fully compatible with upstream Perl 5.42.

See also#

  • last — the loop counterpart; exits the innermost enclosing for / while / foreach / bare block, and unlike break can target an outer block by label

  • next — start the next iteration of a loop rather than exiting it; useful when break is the wrong shape because you want continuation, not termination

  • return — unwinds out of the enclosing subroutine entirely, not just the given block; use when the given is a dispatcher inside a sub and the answer is ready