Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I have reworked the construct as discussed earlier. I include a minimal test suite here to demonstrate the expected behavior.
#!perl use strict; use warnings; use Test::More tests => 13; use Case; my $case = Case::switch( 'foo' => sub {5} ); ok($case, 'create switch'); is($case->('foo'), 5, 'found'); is($case->('bar'), undef, 'default default'); $case = Case::switch( Case::default => sub { 'given' } ); is($case->('foo'), 'given', 'supplied default'); $case = Case::switch( qw(foo bar baz) => sub {'fell thru'}, Case::default => sub {'too far'} ); is($case->('foo'), 'fell thru', 'normal fall-thru'); $case = Case::switch( qw(foo bar) => sub {'one ' . $Case::action->('baz')}, sorbet => sub {'to cleanse the palate'}, baz => sub {'chain'} ); is($case->('foo'), 'one chain', 'chaining'); $case = Case::switch( qw(foo bar) => sub {"got $_"}, qw(baz) => sub {$Case::action->('foo')}, 'roy' => sub { 'special '. $Case::action->('foo')}, Case::default => sub { 'Just wasting space' } ); is($case->('foo'), 'got foo', 'arg is $_'); is($case->('baz'), 'got baz', 'arg is $_ when chained'); is($case->('roy'), 'special got roy', 'cat chained return'); # Weird ones ok(Case::switch(), 'completely empty'); ok(Case::switch(sub{}), 'default only'); ok(Case::switch('foo'), 'term, no sub'); diag("Should get a malformed switch warning"); ok(Case::switch(sub{}, sub{}), 'malformed switch warning');
and the module itself, which is more lightweight and (I think) elegant than before:
package Case; use Carp; use Exporter; @ISA=(Exporter); @EXPORT_OK=qw(switch default); use strict; use warnings; sub default {} sub switch { my %swash; my $default = \&default; my $code; my $assigned_code; # Handle degenerate case return $default if (@_ == 0); # Handle default if ((ref $_[-1]) eq 'CODE' and (@_ ==1 or ref $_[-2] eq 'CODE')) { $default = pop(@_); } for my $item (reverse @_) { if ((my $reftype = ref $item) eq 'CODE') { carp "Malformed switch: action with no terms" if $code and ! $assigned_code; $code = $item; $assigned_code = 0; } elsif ($reftype) { croak "switch cannot handle $reftype-ref arguments"; } else { $swash{$item} = $code; ++$assigned_code; } } carp "Malformed switch: action with no terms" if $code and ! $assigned_code; return sub { local($_) = @_; local $Case::action = sub { ($swash{$_[0]} || $default)->() }; &$Case::action; }; } 1;

Caution: Contents may have been coded under pressure.

In reply to Revision 2: Switch/case as a dispatch table with C-style fall-through by Roy Johnson
in thread 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":



  • 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 pondering the Monastery: (6)
As of 2024-03-28 14:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found