File Coverage

blib/lib/Dispatch/Fu.pm
Criterion Covered Total %
statement 27 27 100.0
branch 3 4 75.0
condition 6 8 75.0
subroutine 8 8 100.0
pod 4 4 100.0
total 48 51 94.1


line stmt bran cond sub pod time code
1             package Dispatch::Fu;
2              
3 3     3   86814 use strict;
  3         18  
  3         88  
4 3     3   15 use warnings;
  3         4  
  3         78  
5 3     3   13 use Exporter qw/import/;
  3         6  
  3         1115  
6              
7             our $VERSION = q{0.99};
8             our @EXPORT = qw(dispatch on cases xdefault);
9             our @EXPORT_OK = qw(dispatch on cases xdefault);
10              
11             my $DISPATCH_TABLE = {};
12              
13             sub dispatch (&@) {
14 105     105 1 152 my $code_ref = shift; # catch sub ref that was coerced from the 'dispatch' BLOCK
15 105         134 my $match_ref = shift; # catch the input reference passed after the 'dispatch' BLOCK
16              
17             # build up dispatch table for each k/v pair preceded by 'on'
18 105         269 while ( my $key = shift @_ ) {
19 635         1180 my $HV = shift @_;
20 635         848 $DISPATCH_TABLE->{$key} = _to_sub($HV);
21             }
22              
23             # call $code_ref that needs to return a valid bucket name
24 105         233 my $key = $code_ref->($match_ref);
25              
26 105 50 33     51766 die qq{Computed static bucket not found\n} if not $DISPATCH_TABLE->{$key} or 'CODE' ne ref $DISPATCH_TABLE->{$key};
27              
28             # call subroutine ref defined as the v in the k/v $DISPATCH_TABLE->{$key} slot
29 105         201 my $sub_to_call = $DISPATCH_TABLE->{$key};
30              
31             # reset table
32 105         283 $DISPATCH_TABLE = {};
33              
34             # dispatch with $match_ref
35 105         268 $sub_to_call->($match_ref);
36             }
37              
38             # on accumulater, wants h => v pair, where h is a static bucket string and v is a sub ref
39             sub on (@) {
40 635     635 1 103070 return @_;
41             }
42              
43             # sub for introspection, returns the string names of each case
44             # added using the C keyword
45             sub cases () {
46 201     201 1 1876 return keys %$DISPATCH_TABLE;
47             }
48              
49             # if $case is in cases(), return $case; otherwise return $default
50             # Note: $default defaults to q{default}; i.e., if the name of the
51             # default case is not specified, the string 'default' is returned
52             sub xdefault($;$) {
53 5     5 1 26 my ($case, $default) = @_;
54 5 100 100     16 if ($case and grep { /$case/ } (cases)){
  21         71  
55 1         3 return $case;
56             }
57 4   100     15 return $default // q{default};
58             }
59              
60             # utility sub to force a BLOCK into a sub reference
61             sub _to_sub (&) {
62 635     635   1563 shift;
63             }
64              
65             1;
66              
67             __END__