File Coverage

blib/lib/Dispatch/Fu.pm
Criterion Covered Total %
statement 22 22 100.0
branch 1 2 50.0
condition 1 3 33.3
subroutine 7 7 100.0
pod 3 3 100.0
total 34 37 91.8


line stmt bran cond sub pod time code
1             package Dispatch::Fu;
2              
3 2     2   70592 use strict;
  2         10  
  2         62  
4 2     2   9 use warnings;
  2         4  
  2         53  
5 2     2   10 use Exporter qw/import/;
  2         3  
  2         613  
6              
7             our $VERSION = q{0.97};
8             our @EXPORT = qw(dispatch on cases);
9             our @EXPORT_OK = qw(dispatch on cases);
10              
11             my $DISPATCH_TABLE = {};
12              
13             sub cases () {
14 198     198 1 1834 return keys %$DISPATCH_TABLE;
15             }
16              
17             sub dispatch (&@) {
18 100     100 1 145 my $code_ref = shift; # catch sub ref that was coerced from the 'dispatch' BLOCK
19 100         130 my $match_ref = shift; # catch the input reference passed after the 'dispatch' BLOCK
20              
21             # build up dispatch table for each k/v pair preceded by 'on'
22 100         279 while ( my $key = shift @_ ) {
23 600         772 my $HV = shift @_;
24 600         788 $DISPATCH_TABLE->{$key} = _to_sub($HV);
25             }
26              
27             # call $code_ref that needs to return a valid bucket name
28 100         221 my $key = $code_ref->($match_ref);
29              
30 100 50 33     52143 die qq{Computed static bucket not found\n} if not $DISPATCH_TABLE->{$key} or 'CODE' ne ref $DISPATCH_TABLE->{$key};
31              
32             # call subroutine ref defined as the v in the k/v $DISPATCH_TABLE->{$key} slot
33 100         190 my $sub_to_call = $DISPATCH_TABLE->{$key};
34              
35             # reset table
36 100         276 $DISPATCH_TABLE = {};
37              
38 100         213 $sub_to_call->();
39             }
40              
41             # on accumulater, wants h => v pair, where h is a static bucket string and v is a sub ref
42             sub on (@) {
43 600     600 1 102933 return @_;
44             }
45              
46             # utility sub to force a BLOCK into a sub reference
47             sub _to_sub (&) {
48 600     600   1470 shift;
49             }
50              
51             1;
52              
53             __END__