line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
use 5.006; |
3
|
8
|
|
|
8
|
|
3179
|
use strict; |
|
8
|
|
|
|
|
54
|
|
4
|
8
|
|
|
8
|
|
43
|
use warnings; |
|
8
|
|
|
|
|
16
|
|
|
8
|
|
|
|
|
161
|
|
5
|
8
|
|
|
8
|
|
36
|
|
|
8
|
|
|
|
|
17
|
|
|
8
|
|
|
|
|
217
|
|
6
|
|
|
|
|
|
|
#use Data::Dumper; # DEBUG |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
use Guard; |
9
|
8
|
|
|
8
|
|
3564
|
use Sub::Multi::Tiny::Util qw(_hlog _line_mark_string _make_positional_copier |
|
8
|
|
|
|
|
3920
|
|
|
8
|
|
|
|
|
530
|
|
10
|
8
|
|
|
|
|
2804
|
_complete_dispatcher); |
11
|
8
|
|
|
8
|
|
75
|
|
|
8
|
|
|
|
|
18
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.000013'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# Documentation {{{1 |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 NAME |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
Sub::Multi::Tiny::Dispatcher::Default - Default dispatcher-maker for Sub::Multi::Tiny |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 SYNOPSIS |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
require Sub::Multi::Tiny::Dispatcher::Default; |
23
|
|
|
|
|
|
|
my $dispatcher_coderef = |
24
|
|
|
|
|
|
|
Sub::Multi::Tiny::Dispatcher::Default::MakeDispatcher({impls=>[]...}); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
See L<Sub::Multi::Tiny> for more. This module does not export any symbols |
27
|
|
|
|
|
|
|
(or even have the capability to do so!). |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
This dispatcher currently only dispatches by arity. |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=head1 FUNCTIONS |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=cut |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# }}}1 |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=head2 MakeDispatcher |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
Make the default dispatcher for the given multi. See L</SYNOPSIS>. |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
TODO expand. For now, only dispatches based on arity. |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=cut |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
my $hr = shift; # Has possible_params and impls arrayrefs |
46
|
|
|
|
|
|
|
my $code = ''; |
47
|
6
|
|
|
6
|
1
|
17
|
_hlog { require Data::Dumper; |
48
|
6
|
|
|
|
|
15
|
"Making default dispatcher for: ", Data::Dumper->Dump([$hr], ['multisub']) }; |
49
|
3
|
|
|
3
|
|
14
|
|
50
|
6
|
|
|
|
|
45
|
# Sort the candidates |
|
3
|
|
|
|
|
28
|
|
51
|
|
|
|
|
|
|
my (%candidates_by_arity, %copiers_by_arity); # TODO make this cleaner |
52
|
|
|
|
|
|
|
foreach my $impl (@{$hr->{impls}}) { |
53
|
6
|
|
|
|
|
43
|
my $arity = @{$impl->{args}}; |
54
|
6
|
|
|
|
|
17
|
die "Two candidates of the same arity ($arity) - try D:TypeParams?" |
|
6
|
|
|
|
|
28
|
|
55
|
10
|
|
|
|
|
18
|
if exists $candidates_by_arity{$arity}; |
|
10
|
|
|
|
|
34
|
|
56
|
|
|
|
|
|
|
$candidates_by_arity{$arity} = $impl->{code}; |
57
|
10
|
100
|
|
|
|
250
|
$copiers_by_arity{$arity} = |
58
|
9
|
|
|
|
|
28
|
_make_positional_copier($hr->{defined_in}, $impl); |
59
|
|
|
|
|
|
|
|
60
|
9
|
|
|
|
|
41
|
# Die cleanly if we got something we can't handle |
61
|
|
|
|
|
|
|
foreach my $arg (@{$impl->{args}}) { |
62
|
|
|
|
|
|
|
die "Type constraint on $impl->{candidate_name}, arg $arg->{name}" |
63
|
9
|
|
|
|
|
26
|
. '- try D:TypeParams?' if $arg->{type}; |
|
9
|
|
|
|
|
39
|
|
64
|
|
|
|
|
|
|
die "'where' clause on $impl->{candidate_name}, arg $arg->{name}" |
65
|
12
|
100
|
|
|
|
97
|
. '- try D:TypeParams?' if $arg->{where}; |
66
|
|
|
|
|
|
|
} #foreach $arg |
67
|
11
|
100
|
|
|
|
289
|
|
68
|
|
|
|
|
|
|
} #foreach $impl |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
# Make the dispatcher |
71
|
|
|
|
|
|
|
$code .= _line_mark_string <<EOT; |
72
|
|
|
|
|
|
|
# Find the candidate |
73
|
3
|
|
|
|
|
23
|
my \$arity = scalar \@_; |
74
|
|
|
|
|
|
|
\$candidate = \$data[0]->{\$arity}; |
75
|
0
|
|
|
|
|
0
|
die "No candidate found for $hr->{defined_in}\() with arity " . |
76
|
0
|
|
|
|
|
0
|
(scalar \@_) unless \$candidate; |
77
|
0
|
0
|
|
|
|
0
|
\$copier = \$data[1]->{\$arity}; |
78
|
|
|
|
|
|
|
EOT |
79
|
0
|
|
|
|
|
0
|
|
80
|
|
|
|
|
|
|
return _complete_dispatcher($hr, $code, |
81
|
|
|
|
|
|
|
# @data used by $code |
82
|
3
|
|
|
|
|
18
|
\%candidates_by_arity, |
83
|
|
|
|
|
|
|
\%copiers_by_arity |
84
|
|
|
|
|
|
|
); |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
} #MakeDispatcher |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
1; |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# Rest of documentation {{{1 |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head1 AUTHOR |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
Chris White E<lt>cxw@cpan.orgE<gt> |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head1 LICENSE |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
Copyright (C) 2019 Chris White E<lt>cxw@cpan.orgE<gt> |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
101
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=cut |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# }}}1 |
106
|
|
|
|
|
|
|
# vi: set fdm=marker: # |