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

filter: Statementstmt 96.9% 63 / 65 Branchbran 60.0% 6 / 10 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 13 / 13 Podpod 100% 1 / 1 totaltotal 89.3% 84 / 94 CC 9 SCAR 15.4
CC
9
Cov
90%
MakeDispatcher
9.6
BEGIN
1.0
BEGIN
1.0
CRAP 4.7
« support/SigParse.yp ↑ Summary blib/lib/Sub/Multi/Tiny/Dispatcher/Default.pm »
1 package Sub::Multi::Tiny::Dispatcher::TypeParams;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use vars::i '@EXPORT' => qw(MakeDispatcher);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Guard;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Import::Into;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Sub::Multi::Tiny::Util qw(_hlog _line_mark_string _make_positional_copier
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 _complete_dispatcher);
14 2 ▶use Type::Params qw(multisig);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Type::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 our $VERSION = '0.000013';
18  
19 # Documentation {{{1
20  
21 =head1 NAME
22  
23 Sub::Multi::Tiny::Dispatcher::TypeParams - Dispatcher-maker using Type::Params for Sub::Multi::Tiny
24  
25 =head1 SYNOPSIS
26  
27 # In a multisub
28 require Sub::Multi::Tiny qw($param D:TypeParams);
29  
30 # Internals of Sub::Multi::Tiny
31 use Type::Params;
32 my $dispatcher_coderef =
33 Sub::Multi::Tiny::Dispatcher::TypeParams::MakeDispatcher({impls=>[]...});
34  
35 This module dispatches to any function that can be distinguished by the
36 C<multisig> function in L<Type::Params>. See
37 L<Type::Params/MULTIPLE SIGNATURES>.
38  
39 See L<Sub::Multi::Tiny> for more about the usage of this module.
40 This module does not export any symbols.
41  
42 =head1 USAGE NOTES
43  
44 =head2 Candidate order
45  
46 The candidates must be listed with more
47 specific first, since they are tried top to bottom. For example, constraint
48 L<Types::Standard/Str> matches any scalar (as of Types::Standard v1.004004), so
49 it should be listed after more specific constraints such as
50 L<Types::Standard/Int>.
51  
52 =head2 Named parameters
53  
54 C<Type::Parameters::multisig()> does not directly support named parameters.
55 Instead, use a slurpy hash (C<Dict>) parameter to collect named parameters.
56 An example is given in L<Type::Params/Mixed Positional and Named Parameters>.
57  
58 =head1 FUNCTIONS
59  
60 =cut
61  
62 # }}}1
63  
64 =head2 MakeDispatcher
65  
66 Make the default dispatcher for the given multi. See L</SYNOPSIS>.
67  
68 =cut
69  
70 # uniquify constraint names
71 2 my $_constraint_idx = 0;
72  
73 # Our own "any" type
74 2 my $_any_type = Type::Tiny->new(name => 'Any_SMTD_TypeParams');
75 # Default constraint accepts anything
76  
77 1 ▶sub MakeDispatcher {
Subroutine MakeDispatcher: called CC=9 SCAR=22.6
Pod documented
78 1 my $hr = shift; # Has possible_params and impls arrayrefs
79 1 my $code = '';
80 1 ▶ _hlog { require Data::Dumper;
Subroutine __ANON__: called CC=1 SCAR=0.0
81 1 "Making Type::Params dispatcher for: ",
82 1 Data::Dumper->Dump([$hr], ['multisub']) };
83  
84 # Make an array of typechecks for multisig()
85 1 my (@sigs, @impls, @copiers);
86 1 foreach my $impl (@{$hr->{impls}}) {
87 2 my @sig;
88 2 foreach my $param (@{$impl->{args}}) {
89  
90 # Sanity checks. TODO FIXME remove the need for these!
91 die "I don't yet know how to handle named arguments"
92 2 ▶ if $param->{named};
Branch: 1 branch
BranchTrueFalseTotal
if $param->{"named"} 0 2 2
93 die "I don't yet know how to handle optional arguments"
94 2 ▶ if !$param->{reqd};
Branch: 1 branch
BranchTrueFalseTotal
unless $param->{"reqd"} 0 2 2
95  
96 # Make the constraint
97 2 my $constraint;
98 2 ▶ if($param->{type} && $param->{where}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($param->{'type'} and $param->{'where'}) { } 0 2 2
elsif ($param->{'type'}) { } 0 2 2
elsif ($param->{'where'}) { } 1 1 2
Condition33%1/3
$param->{"type"} and $param->{"where"}
!ll&&!rl&&r
200
MC/DC0%0/2
$param->{"type"} and $param->{"where"}
$param->{"type"} $param->{"where"}
Truth table33%1/3
A and B
A= $param->{"type"}B= $param->{"where"}
ABresult
0X0
100
111
99 0 $constraint = $param->{type} & $param->{where};
100 # Subtype - see http://blogs.perl.org/users/toby_inkster/2014/08/typetiny-tricks-1-quick-intersections.html
101 } elsif($param->{type}) {
102 0 $constraint = $param->{type};
103 } elsif($param->{where}) {
104 $constraint = Type::Tiny->new(
105 name => 'Constraint' . $_constraint_idx++ . '_' .
106 substr($param->{name}, 1),
107 constraint => $param->{where},
108 1 );
109 } else { # No constraint
110 1 $constraint = $_any_type;
111 }
112  
113 # Add it to the signature
114 2 push @sig, $constraint;
115 } #foreach param
116  
117 2 push @sigs, [@sig];
118 2 push @impls, $impl->{code};
119  
120 # Use a straight positional copier. This is sufficient even for
121 # named parameters because Type::Params::multisig()
122 # fakes named parameters with a slurpy hash.
123 2 push @copiers, _make_positional_copier($hr->{defined_in}, $impl);
124 } #foreach impl
125  
126 1 my $checker = multisig(@sigs);
127  
128 # Make the dispatcher
129 1 $code .= _line_mark_string <<'EOT';
130 # Find the candidate
131 12 @_ = $data[0]->(@_); # $checker. Dies on error.
132 # NOTE: this change can't be `local`ized because `goto`
133 # undoes the `local` - see #8
134 12 $candidate = $data[1]->[${^_TYPE_PARAMS_MULTISIG}]; # impls
135 12 $copier = $data[2]->[${^_TYPE_PARAMS_MULTISIG}]; # copiers
136 EOT
137  
138 1 return _complete_dispatcher($hr, $code, $checker, \@impls, \@copiers);
139 } #MakeDispatcher
140  
141 =head2 import
142  
143 When used, also imports L<Type::Tiny> into the caller's namespace (since
144 C<Type::Tiny> types are how this dispatcher functions!).
145 The caller may also wish to import L<Types::Standard>, but we don't do so
146 here in the interest of generality.
147  
148 =cut
149  
150 2 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
151 2 my $target = caller;
152 2 __PACKAGE__->export_to_level(1, @_);
153 2 Type::Tiny->import::into($target);
154 }
155  
156 2 1;
157 __END__
« support/SigParse.yp ↑ Summary blib/lib/Sub/Multi/Tiny/Dispatcher/Default.pm »