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/elsechain — direct replacement for mostgiven/whenusage: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
elsifladder 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 thatbreakprovided, wrap the body in a bare block andlastout 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.
breaktakes nothing.break LABELis a syntax error — unlikelast/next,breakhas no way to target an outer block.Outside
given. Callingbreak(orCORE::break) when nogivenblock is on the call stack raisesCan't "break" outside a given block.returnwins. Inside a subroutine,returnunwinds the whole sub regardless of any enclosinggiven.breakonly unwinds to the end of thegiven; control continues in the surrounding sub.Implicit
breakat end ofwhen. Awhenclause’s body ends with an implicitbreakunless the body iscontinue, so an explicitbreakat the end of awhenbody is redundant.continuesuppresses the implicitbreakand lets control fall through to the nextwhen.Not a loop keyword.
breakdoes not interact withfor,while,foreach,do, or bare blocks. For those, uselast.Experimental warnings. Running
given/whencode withoutno warnings 'experimental::smartmatch'prints the warning once per smartmatch site, which in a busygivenblock can be every iteration.
Differences from upstream#
Fully compatible with upstream Perl 5.42.
See also#
last— the loop counterpart; exits the innermost enclosingfor/while/foreach/ bare block, and unlikebreakcan target an outer block by labelnext— start the next iteration of a loop rather than exiting it; useful whenbreakis the wrong shape because you want continuation, not terminationreturn— unwinds out of the enclosing subroutine entirely, not just thegivenblock; use when thegivenis a dispatcher inside a sub and the answer is ready