http://www.perlmonks.org?node_id=1054959


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

Replies are listed 'Best First'.
Re^3: switch statement
by ikegami (Patriarch) 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.