Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

This is a very good answer. I think the important point that may get lost in the excellent historic detail is this:

Switch (the module) has been deprecated and shouldn't be used in production code.

switch (given/when) has been marked "experimental", and shouldn't be used in production code.

Perl doesn't have a switch type statement that should be used in production code.

perlfaq7 lists several options for "case" or "switch" statement emulation. My favorite from this document is the hash-based dispatch table. Apparently the document hasn't been updated to reflect that given/when are experimental, so just ignore that option. The others ought to work fine though. Keep in mind that in Perl's history up until 5.10 there simply didn't exist a case or switch type syntax, and people did fine without it.

One other option

my @dispatch = ( [ sub { shift =~ m/test/ } => sub { action } ], [ sub { shift == 2 } => sub { action } ], [ sub { shift ge 'Hello' } => sub { action } ], ); foreach my $case ( @dispatch ) { if( $case->[0]($test_value) ) { $case->[1](); # Action. last; } }

There's a sequential dispatch table that uses callbacks for the test, and callbacks for the action. If the tests can be made uniform enough the callback wouldn't be necessary, but I used it to demonstrate a very generalized solution. I think it would become more legible by using List::MoreUtils::firstidx, but that's really just syntactic sugar; it's the same algorithm either way.

Update: Here's a List::MoreUtils::firstidx solution (untested):

my @dispatch = ( ... same as above ... ); $dispatch[ # In the table... firstidx { $_->[0]($test_value) } @dispatch # do a lookup... ]->[1](); # and take action.

Hmm.... naw, that's cluttered. How about List::Util::first:

my @dispatch = ( ...same as above ... ); ( first { $_->[0]($test_value) } @dispatch )->[1]();

Much better. ;)


Dave


In reply to Re^2: switch statement by davido
in thread switch statement by virtuemart2

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
  • Outside of code tags, you may need to use entities for some characters:
            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 taking refuge in the Monastery: (6)
    As of 2014-12-28 02:11 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (177 votes), past polls