I plan to make a collection of various flavors of case/switch/given and package them up for CPAN as one package, probably named "Case.pm". This one is very C-flavored, with a twist. It constructs a hash associating your options with their actions, and returns a closure (necessary for handling default and fall-through). Then, when you need to execute the case, you just pass your term to the closure, and the appropriate sub(s) are executed.

I even wrangled a break() statement. No default identifier, though.

Comments on flaws or possible enhancements* would be welcome.

* within the limitations of using a hash jump table


Revision 1: Revision 2:(posted as followup, below)
package Swash; # This is a tag you can stick in the list for clarity # also serves as default routine when none is specified sub default {} # Make a key for each non-ref entry; the value is a coderef # that executes the first subsequent coderef in the list # Plus some bookkeeping to make fallthrough happen sub new { my %swash; my ($default, $nextcode, $gotcode) = (\&default) x 2; my $call_pkg = caller; for my $item (reverse @_) { if (ref $item eq 'CODE') { if ($gotcode) { if (keys %swash) { use Carp; croak "Malformed swash: non-default coderef with n +o associated term found";} $nextcode = $default = $gotcode; } $gotcode = $item; } elsif ($gotcode) { my ($case_code, $fallthru_code) = ($gotcode, $nextcode); $swash{$item} = sub { my $fallthru = 1; no strict 'refs'; no warnings 'redefine'; local *{$call_pkg.'::break'} = sub { $fallthru = 0 + }; $case_code->($_[0]); $fallthru_code->($_[0]) if $fallthru; }; $nextcode = $swash{$item}; ($gotcode, @keys) = (); } else { $swash{$item} = $nextcode; } } return sub { my ($term) = @_; ($swash{$term} || $default)->($term) + }; } 1; package main; my $case = Swash::new( qw(mozart) => sub { print "$_[0] was a Musician!\n" }, qw(einstein newton) => sub { print "$_[0] was a Genius!\n"; break() +; }, qw(dog cat pig) => sub { print "$_[0] is an Animal!\n"; break() +; }, 'Roy' => sub { print "$_[0] should fall through..." } +, Swash::default => sub { print "No idea what $_[0] is.\n" } ); for (qw(mozart cat PerlMonk newton pig einstein)) { print "Looking up $_...\n"; $case->($_); } print "And Roy?\n"; $case->('Roy');

In reply to Switch/case as a dispatch table with C-style fall-through by Roy Johnson

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.