line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Dispatch::Fu; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
111835
|
use strict; |
|
3
|
|
|
|
|
20
|
|
|
3
|
|
|
|
|
87
|
|
4
|
3
|
|
|
3
|
|
15
|
use warnings; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
81
|
|
5
|
3
|
|
|
3
|
|
15
|
use Exporter qw/import/; |
|
3
|
|
|
|
|
10
|
|
|
3
|
|
|
|
|
1253
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = q{0.98}; |
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
|
103
|
|
|
103
|
1
|
151
|
my $code_ref = shift; # catch sub ref that was coerced from the 'dispatch' BLOCK |
15
|
103
|
|
|
|
|
133
|
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
|
103
|
|
|
|
|
265
|
while ( my $key = shift @_ ) { |
19
|
621
|
|
|
|
|
820
|
my $HV = shift @_; |
20
|
621
|
|
|
|
|
838
|
$DISPATCH_TABLE->{$key} = _to_sub($HV); |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# call $code_ref that needs to return a valid bucket name |
24
|
103
|
|
|
|
|
226
|
my $key = $code_ref->($match_ref); |
25
|
|
|
|
|
|
|
|
26
|
103
|
50
|
33
|
|
|
52532
|
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
|
103
|
|
|
|
|
185
|
my $sub_to_call = $DISPATCH_TABLE->{$key}; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# reset table |
32
|
103
|
|
|
|
|
275
|
$DISPATCH_TABLE = {}; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# dispatch with $match_ref |
35
|
103
|
|
|
|
|
262
|
$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
|
621
|
|
|
621
|
1
|
104144
|
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
|
1893
|
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
|
3
|
|
|
3
|
1
|
15
|
my ($case, $default) = @_; |
54
|
3
|
100
|
|
|
|
7
|
if (grep { /$case/ } (cases)){ |
|
21
|
|
|
|
|
69
|
|
55
|
1
|
|
|
|
|
4
|
return $case; |
56
|
|
|
|
|
|
|
} |
57
|
2
|
|
100
|
|
|
10
|
return $default // q{default}; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
# utility sub to force a BLOCK into a sub reference |
61
|
|
|
|
|
|
|
sub _to_sub (&) { |
62
|
621
|
|
|
621
|
|
1515
|
shift; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
1; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
__END__ |