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.given($data) { when (1) { say 'one' ; }; ## no "break" needed when (2) { say '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 code does not work ! # it will say "something else" given($data) { when (1) { continue; }; when (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.# this works, is shorter and easier to read given($data) { when ([ 1, 2]) { say 'one or two' ; }; default { say 'something else'; }; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Falling through Perl 5.10 switch statement
by betterworld (Curate) on Apr 07, 2007 at 15:03 UTC | |
by Trizor (Pilgrim) on Apr 07, 2007 at 20:33 UTC | |
by ikegami (Patriarch) on Apr 07, 2007 at 21:37 UTC | |
by betterworld (Curate) on Apr 09, 2007 at 02:03 UTC | |
by ysth (Canon) on Apr 08, 2007 at 06:09 UTC | |
by Anonymous Monk on Apr 16, 2007 at 08:01 UTC |