blib/lib/Sub/Multi/Tiny/Dispatcher/Default.pm

filter: Statementstmt 88.5% 31 / 35 Branchbran 100% 6 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 85.7% 6 / 7 Podpod 100% 1 / 1 totaltotal 89.8% 44 / 49 CC 6 SCAR 12.8
CC
6
Cov
90%
MakeDispatcher
6.1
__ANON__
1.3
BEGIN
1.0
CRAP 3.6
« blib/lib/Sub/Multi/Tiny/Dispatcher/TypeParams.pm ↑ Summary blib/lib/Sub/Multi/Tiny/Util.pm »
1 package Sub::Multi::Tiny::Dispatcher::Default;
2  
3 5 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 #use Data::Dumper; # DEBUG
8  
9 5 ▶use Guard;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Sub::Multi::Tiny::Util qw(_hlog _line_mark_string _make_positional_copier
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 _complete_dispatcher);
12  
13 5 our $VERSION = '0.000013';
14  
15 # Documentation {{{1
16  
17 =head1 NAME
18  
19 Sub::Multi::Tiny::Dispatcher::Default - Default dispatcher-maker for Sub::Multi::Tiny
20  
21 =head1 SYNOPSIS
22  
23 require Sub::Multi::Tiny::Dispatcher::Default;
24 my $dispatcher_coderef =
25 Sub::Multi::Tiny::Dispatcher::Default::MakeDispatcher({impls=>[]...});
26  
27 See L<Sub::Multi::Tiny> for more. This module does not export any symbols
28 (or even have the capability to do so!).
29  
30 This dispatcher currently only dispatches by arity.
31  
32 =head1 FUNCTIONS
33  
34 =cut
35  
36 # }}}1
37  
38 =head2 MakeDispatcher
39  
40 Make the default dispatcher for the given multi. See L</SYNOPSIS>.
41  
42 TODO expand. For now, only dispatches based on arity.
43  
44 =cut
45  
46 3 ▶sub MakeDispatcher {
Subroutine MakeDispatcher: called CC=6 SCAR=18.2
Pod documented
47 3 my $hr = shift; # Has possible_params and impls arrayrefs
48 3 my $code = '';
49 0 ▶ _hlog { require Data::Dumper;
Subroutine __ANON__: not called CC=1 SCAR=2.6
50 0 "Making default dispatcher for: ", Data::Dumper->Dump([$hr], ['multisub']) };
51  
52 # Sort the candidates
53 3 my (%candidates_by_arity, %copiers_by_arity); # TODO make this cleaner
54 3 foreach my $impl (@{$hr->{impls}}) {
55 4 my $arity = @{$impl->{args}};
56 die "Two candidates of the same arity ($arity) - try D:TypeParams?"
57 4 ▶ if exists $candidates_by_arity{$arity};
Branch: 1 branch
BranchTrueFalseTotal
if exists $candidates_by_arity{$arity} 1 3 4
58 3 $candidates_by_arity{$arity} = $impl->{code};
59 $copiers_by_arity{$arity} =
60 3 _make_positional_copier($hr->{defined_in}, $impl);
61  
62 # Die cleanly if we got something we can't handle
63 3 foreach my $arg (@{$impl->{args}}) {
64 die "Type constraint on $impl->{candidate_name}, arg $arg->{name}"
65 3 ▶ . '- try D:TypeParams?' if $arg->{type};
Branch: 1 branch
BranchTrueFalseTotal
if $arg->{"type"} 1 2 3
66 die "'where' clause on $impl->{candidate_name}, arg $arg->{name}"
67 2 ▶ . '- try D:TypeParams?' if $arg->{where};
Branch: 1 branch
BranchTrueFalseTotal
if $arg->{"where"} 1 1 2
68 } #foreach $arg
69  
70 } #foreach $impl
71  
72 # Make the dispatcher
73 0 $code .= _line_mark_string <<EOT;
74 # Find the candidate
75 my \$arity = scalar \@_;
76 \$candidate = \$data[0]->{\$arity};
77 die "No candidate found for $hr->{defined_in}\() with arity " .
78 (scalar \@_) unless \$candidate;
79 \$copier = \$data[1]->{\$arity};
80 EOT
81  
82 0 return _complete_dispatcher($hr, $code,
83 # @data used by $code
84 \%candidates_by_arity,
85 \%copiers_by_arity
86 );
87  
88 } #MakeDispatcher
89  
90 5 1;
91 __END__
« blib/lib/Sub/Multi/Tiny/Dispatcher/TypeParams.pm ↑ Summary blib/lib/Sub/Multi/Tiny/Util.pm »