In Control flow§
See primary documentation in context for default and when
A block containing a default
statement will be left immediately when the sub-block after the default
statement is left. It is as though the rest of the statements in the block were skipped.
given 42# The above block evaluates to 43
A when
statement will also do this (but a when
statement modifier will not.)
In addition, when
statements smartmatch
the topic ($_
) against a supplied expression such that it is possible to check against values, regular expressions, and types when specifying a match.
for 42, 43, "foo", 44, "bar"# OUTPUT: «4243Not an Int or a Bar44Bar»
In this form, the given
/when
construct acts much like a set of if
/elsif
/else
statements. Be careful with the order of the when
statements. The following code says "Int"
not 42
.
given 42# OUTPUT: «Int»
When a when
statement or default
statement causes the outer block to return, nesting when
or default
blocks do not count as the outer block, so you can nest these statements and still be in the same "switch" just so long as you do not open a new block:
given 42# OUTPUT: «42»
when
statements can smartmatch against Signatures.