|Syntactic Confectionery Delight|
Re^2: switch statementby davido (Archbishop)
|on Sep 20, 2013 at 05:35 UTC||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
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):
Hmm.... naw, that's cluttered. How about List::Util::first:
Much better. ;)