Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Basically, you're trying to come up with an API for a generalized switch statement without using source filters in Perl5. The actual mechanics, frankly, are irrelevant.

While I don't have much in the way of implementation suggestions, I do have a few requirements that I'd like to see from a switch statement. You may already implement some of these - I haven't checked.

  • Ordering of cases. This is very important with fall-through, because I may only want fall-through to a specific location. So, something like:
    switch (i) { 0,1: foo(); 2: bar(); break; 3: baz(); break; };
    0 and 1 must fall through to 2, but not 3. It also matters with the case of regex matches (q.v. below).
  • Smart matching. If I give you '5' as a case, I want '05' to match it. If I give you qr/^boo.*boo$/, I want you to match 'booboo' and 'booBOOboo'. If I give you a code reference, I want you to pass the value to the coderef which will return true or false. And, so forth.
  • Options for how matching happens. I might want a switch statement to be case-sensitive or case-insensitive. I might want matches to always be string-matches (in which case '5' and '05' are different) or I might want them to be smart.
  • I want a redo statement in my switch. This is actually very useful in parsing, particularly something like CSV data. Text::xSV basically implements a switch statement with redo as its parsing engine. (Oh, and the cases are primarily regexes.)

Taking a page from Class::MakeMethods, it might be useful to have each case be an arrayref. The last value is the action to take. All others are the case options. If the first value begins with '--', then it's an optional modifier to how to handle the next case option. So, maybe something like:

my $switch = Switch->new( [ '5', sub {} ], # Matches '005' [ '--string-match', '6', sub {} ], # Doesn't match '006', [ qr/floober/, sub {} ] # regex match [ sub { $foo->bar( @_ ) }, sub {} ] # coderef [ 1 .. 3, 8 .. 20, sub {} ] # Multiple cases );

That would be a useful module.


  • In general, if you think something isn't in Perl, try it out, because it usually is. :-)
  • "What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?"

In reply to Re: Switch/case as a jump table with C-style fall-through by dragonchild
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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (11)
    As of 2015-07-03 10:45 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (51 votes), past polls