Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.

In reply to Falling through Perl 5.10 switch statement by Thilosophy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-04-26 08:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found