Just downloaded 5.9.4 and love the new
switch statement (especially the smart matching that comes with it).
A notable difference from C traditions is that you do not need to
break out of the block to avoid falling through to the next </code>case</code>.
given($data) {
when (1) { say 'one' ; }; ## no "break" needed
when (2) { say 'two' ; };
default { say 'something else'; };
}
The Perldoc states that you can still fall through to
the next
when using
continue.
It should probably point out that this is fundamentally
different from falling through in C, because the following
condition(s) are still being checked.
# this code does not work !
# it will say "something else"
given($data) {
when (1) { continue; };
when (2) { say 'one or two' ; };
default { say 'something else'; };
}
Of course, above example makes not much sense, but a straight port from C code would probably end up like that.
The Perl way to write it (using smart matching) is more like
# this works, is shorter and easier to read
given($data) {
when ([ 1, 2]) { say 'one or two' ; };
default { say 'something else'; };
}
The Imp of the Perverse still wonders if it is possible
to directly "goto" (maybe that's a hint right there...)
the next block, skipping the when check.