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

Re^2: switch statement

by davido (Archbishop)
on Sep 20, 2013 at 05:35 UTC ( #1054959=note: print w/ replies, xml ) Need Help??


in reply to Re: switch statement
in thread switch statement

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


Comment on Re^2: switch statement
Select or Download Code
Re^3: switch statement
by ikegami (Pope) on Mar 18, 2014 at 13:37 UTC

    Switch has not been deprecated. Only its inclusion in core was deprecated. I advise against using Switch as it can result in hard to debug errors, but it's wrong to claim it has been deprecated.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1054959]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (14)
As of 2014-07-25 19:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (174 votes), past polls