Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Here are some others thoughts:

In your current implementation you define the break function in the caller's package, but what if there is already a break function in it?

Wouldn't it be better to allow a user-defined name, and/or return a blessed version of the coderef and make it possible to call the break method on it? (in order to this to work properly you will need to pass the object itself to the code-ref of the eval)

And shouldn't the break function stop the execution of the code? (although this might be rather complicated..., as in adding 'last' to the break sub won't work if the code has it's loop, using die/eval might work but that would be slower...)

And what about implementing next, redo and last?

Where next could mean, jump to the next case statement (that is the fallthru one, possibile with or without ignoring the fallthru variable (if break doesn't return immediatly))

Redo could mean redo the case-coderef. (Updated (old text: entire switch statement, this might be useful when a certain case code-ref modifies the input and wants to re-check it...)

And last could be an alias to break...

By creating your own loop you can figure out which one was called... Some code to demonstrate:

my ($redo, $next, $last)=(0, 0, 1); { my $first = 0; for (0, 1) { if (not $_ and $first++) { $redo=1; $last = 0; last; } if ($_) { $next = 1; $last = 0; last; } main_code(); $last = 0; last; } } $,=" & "; print ($next, $redo, $last); sub main_code { redo; } # replace redo with next and/or last to test


In reply to Re: Switch/case as a dispatch table with C-style fall-through by Animator
in thread Switch/case as a dispatch table with C-style fall-through by Roy Johnson

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 musing on the Monastery: (8)
As of 2024-03-28 09:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found