Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://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":



  • 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 drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-16 12:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found