| 1 | package Sub::Multi::Tiny; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 6 | require Attribute::Handlers; # Listed here so automated tools see it | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use Import::Into; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use Scalar::Util qw(looks_like_number); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use Sub::Multi::Tiny::SigParse; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 6 | ▶ | use Sub::Multi::Tiny::Util ':all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 6 | ▶ | use subs (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 6 | ▶ | use vars (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 6 | our $VERSION = '0.000013'; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 6 | ▶ | use constant { true => !!1, false => !!0 }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # Documentation {{{1 | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =encoding utf-8 | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =head1 NAME | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | Sub::Multi::Tiny - Multisubs/multimethods (multiple dispatch) yet another way! | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | { | ||||||||||||||||||||||||||||
| 31 | package main::my_multi; # We're making main::my_multi() | ||||||||||||||||||||||||||||
| 32 | use Sub::Multi::Tiny qw($foo $bar); # All possible params | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | sub first :M($foo, $bar) { # sub's name will be ignored, | ||||||||||||||||||||||||||||
| 35 | return $foo ** $bar; # but can't match the one we're making | ||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | sub second :M($foo) { | ||||||||||||||||||||||||||||
| 39 | return $foo + 42; | ||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # Back in package main, my_multi() is created just before the run phase. | ||||||||||||||||||||||||||||
| 45 | say my_multi(2, 5); # -> 32 | ||||||||||||||||||||||||||||
| 46 | say my_multi(1295); # -> 1337 | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | The default dispatcher dispatches solely by arity, and only one | ||||||||||||||||||||||||||||
| 49 | candidate can have each arity. For more flexible dispatching, see | ||||||||||||||||||||||||||||
| 50 | L<Sub::Multi::Tiny::Dispatcher::TypeParams>. | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | Sub::Multi::Tiny is a library for making multisubs, aka multimethods, | ||||||||||||||||||||||||||||
| 55 | aka multiple-dispatch subroutines. Each multisub is defined in a | ||||||||||||||||||||||||||||
| 56 | single package. Within that package, the individual implementations ("impls") | ||||||||||||||||||||||||||||
| 57 | are C<sub>s tagged with the C<:M> attribute. The names of the impls are | ||||||||||||||||||||||||||||
| 58 | preserved but not used specifically by Sub::Multi::Tiny. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | Within a multisub package, the name of the sub being defined is available | ||||||||||||||||||||||||||||
| 61 | for recursion. For example (using C<where>, supported by | ||||||||||||||||||||||||||||
| 62 | L<Sub::Multi::Tiny::Dispatcher::TypeParams>): | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | { | ||||||||||||||||||||||||||||
| 65 | package main::fib; | ||||||||||||||||||||||||||||
| 66 | use Sub::Multi::Tiny qw(D:TypeParams $n); | ||||||||||||||||||||||||||||
| 67 | sub base :M($n where { $_ <= 1 }) { 1 } | ||||||||||||||||||||||||||||
| 68 | sub other :M($n) { $n * fib($n-1) } | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | This code creates function C<fib()> in package C<main>. Within package | ||||||||||||||||||||||||||||
| 72 | C<main::fib>, function C<fib()> is an alias for C<main::fib()>. It's easier | ||||||||||||||||||||||||||||
| 73 | to use than to explain! | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | =cut | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # }}}1 | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | # Information about the multisubs so we can create the dispatchers at | ||||||||||||||||||||||||||||
| 82 | # INIT time. | ||||||||||||||||||||||||||||
| 83 | 6 | my %_multisubs; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # Sanity check: any :M will die after the INIT block below runs. | ||||||||||||||||||||||||||||
| 86 | my $_dispatchers_created; | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # Accessor | ||||||||||||||||||||||||||||
| 89 | 8 | ▶ | sub _dispatchers_created { !!$_dispatchers_created; } | ||||||||||||||||||||||||||
|
Subroutine
_dispatchers_created: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # INIT: Fill in the dispatchers for any multisubs we've created. | ||||||||||||||||||||||||||||
| 92 | # Note: attributes are applied at CHECK time, before this. | ||||||||||||||||||||||||||||
| 93 | # We use INIT so that compilation failures will prevent this code from running. | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 5 | ▶ | INIT { | ||||||||||||||||||||||||||
|
Subroutine
INIT: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 96 | 5 | ▶ | _hlog { __PACKAGE__, "in INIT block" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 97 | 5 | $_dispatchers_created = 1; | |||||||||||||||||||||||||||
| 98 | 5 | ▶ | while(my ($multisub_fullname, $hr) = each(%_multisubs)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 4 | ▶ | my $dispatcher = _make_dispatcher($hr) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | or die "Could not create dispatcher for $multisub_fullname\()"; | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 6 | ▶ | eval { no strict 'refs'; *{$multisub_fullname} = $dispatcher }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 103 | 1 | ▶ | die "Could not assign dispatcher for $multisub_fullname\:\n$@" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 1 | do { | |||||||||||||||||||||||||||
| 105 | 6 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 106 | 6 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 107 | 1 | my $target_name = "$hr->{defined_in}\::$hr->{subname}"; | |||||||||||||||||||||||||||
| 108 | 1 | *{$target_name} = $dispatcher; | |||||||||||||||||||||||||||
| 109 | }; | ||||||||||||||||||||||||||||
| 110 | } #foreach multisub | ||||||||||||||||||||||||||||
| 111 | } #CHECK | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =head2 import | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | Sets up the package that uses it to define a multisub. The parameters | ||||||||||||||||||||||||||||
| 116 | are all the parameter variables that the multisubs will use. C<import> | ||||||||||||||||||||||||||||
| 117 | creates these as package variables so that they can be used unqualified | ||||||||||||||||||||||||||||
| 118 | in the multisub implementations. | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | A parameter C<D:Dispatcher> can also be given to specify the dispatcher to | ||||||||||||||||||||||||||||
| 121 | use --- see L</CUSTOM DISPATCH>. | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | Also sets L<Sub::Multi::Tiny::Util/$VERBOSE> if the environment variable | ||||||||||||||||||||||||||||
| 124 | C<SUB_MULTI_TINY_VERBOSE> has a truthy value. If the C<SUB_MULTI_TINY_VERBOSE> | ||||||||||||||||||||||||||||
| 125 | value is numeric, C<$VERBOSE> is set to that value; otherwise, C<$VERBOSE> is | ||||||||||||||||||||||||||||
| 126 | set to 1. | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =cut | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 6 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=19 SCAR=31.3
| |||||||||||||||||||||||||||||
| 131 | 6 | my $multi_package = caller; # The package that defines the multisub | |||||||||||||||||||||||||||
| 132 | 6 | my $my_package = shift; # The package we are | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 6 | for($ENV{SUB_MULTI_TINY_VERBOSE}) { | |||||||||||||||||||||||||||
| 135 | 6 | ▶ | last unless $_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 0 | ▶ | $VERBOSE = looks_like_number($_) ? 0+ $_ : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 6 | ▶ | if(@_ && $_[0] eq ':nop') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ and $_[0] eq ":nop"
MC/DC50%1/2
@_ and $_[0] eq ":nop"
@_
$_[0] eq ":nop"
Truth table66%2/3
A and B
A= @_B= $_[0] eq ":nop"
| |||||||||||||||||||||||||||||
| 140 | 0 | ▶ | _hlog { __PACKAGE__ . ':nop => Taking no action' } 0; # Always | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 141 | 1 | return; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 5 | ▶ | _hlog { "Target $multi_package package $my_package" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 145 | 5 | my ($target_package, $subname) = ($multi_package =~ m{^(.+?)::([^:]+)$}); | |||||||||||||||||||||||||||
| 146 | # $target_package is the package that will be able to call the multisub | ||||||||||||||||||||||||||||
| 147 | 5 | ▶ | _croak "Can't parse package name ${multi_package} into <target>::<name>" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$target_package and $subname
MC/DC0%0/2
$target_package and $subname
$target_package
$subname
Truth table33%1/3
A and B
A= $target_packageB= $subname
| |||||||||||||||||||||||||||||
| 148 | unless $target_package && $subname; | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | _croak "Can't redefine multi sub $multi_package\()" | ||||||||||||||||||||||||||||
| 151 | 5 | ▶ | if exists $_multisubs{$multi_package}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | # Create the vars - they will be accessed as package variables. | ||||||||||||||||||||||||||||
| 154 | # TODO: parameters of the form D:<foo> import dispatcher <foo>. | ||||||||||||||||||||||||||||
| 155 | 5 | my @possible_params; | |||||||||||||||||||||||||||
| 156 | 5 | my $dispatcher = 'Default'; | |||||||||||||||||||||||||||
| 157 | 5 | foreach (@_) { | |||||||||||||||||||||||||||
| 158 | 6 | ▶ | if(/^D:(.*)$/) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 159 | 1 | ▶ | die '"D:" must be followed by a dispatcher class' unless $1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 1 | $dispatcher=$1; | |||||||||||||||||||||||||||
| 161 | } elsif(/^.:/) { | ||||||||||||||||||||||||||||
| 162 | 0 | die '".:..." forms reserved - did you mean "D:DispatcherClass"?'; | |||||||||||||||||||||||||||
| 163 | } else { | ||||||||||||||||||||||||||||
| 164 | 5 | push @possible_params, $_; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | # Load the parameter variables as package variables | ||||||||||||||||||||||||||||
| 169 | 5 | ▶ | _croak "Please list the sub parameters" unless @possible_params; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 5 | vars->import::into($multi_package, @possible_params); | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | # Load the dispatcher | ||||||||||||||||||||||||||||
| 173 | 5 | ▶ | $dispatcher = __PACKAGE__ . "::Dispatcher::$dispatcher" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | unless index($dispatcher, '::') != -1; | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 5 | eval "require $dispatcher"; | |||||||||||||||||||||||||||
| 177 | 5 | ▶ | die "Could not load dispatcher $dispatcher, requested by $multi_package: $@" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | if $@; | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 5 | ▶ | _hlog { $multi_package, 'using dispatcher', $dispatcher }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 181 | 5 | ${dispatcher}->import::into($multi_package); | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | # Make a stub that we will redefine later | ||||||||||||||||||||||||||||
| 184 | 5 | ▶ | _hlog { "Making $multi_package\()" } ; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 185 | 5 | subs->import::into($target_package, $subname); | |||||||||||||||||||||||||||
| 186 | # TODO add stub for callsame/nextwith/... | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | # Save the patch | ||||||||||||||||||||||||||||
| 189 | $_multisubs{$multi_package} = { | ||||||||||||||||||||||||||||
| 190 | used_by => $target_package, | ||||||||||||||||||||||||||||
| 191 | defined_in => $multi_package, | ||||||||||||||||||||||||||||
| 192 | subname => $subname, | ||||||||||||||||||||||||||||
| 193 | 5 | possible_params => +{ map { ($_ => 1) } @possible_params }, | |||||||||||||||||||||||||||
| 194 | impls => [], # Implementations - subs tagged :M | ||||||||||||||||||||||||||||
| 195 | }; | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | # Set up the :M attribute in $multi_package if it doesn't | ||||||||||||||||||||||||||||
| 198 | # exist yet. | ||||||||||||||||||||||||||||
| 199 | 6 | ▶ | if(eval { no strict 'refs'; defined &{$multi_package . '::M'} }) { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | 0 | die "Cannot redefine M in $multi_package"; | |||||||||||||||||||||||||||
| 201 | } else { | ||||||||||||||||||||||||||||
| 202 | 5 | ▶ | _hlog { "Making $multi_package attr M" } 2; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 203 | 5 | eval(_make_M($multi_package)); | |||||||||||||||||||||||||||
| 204 | 5 | ▶ | die $@ if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | # Set up $subname() in $multi_package, which will be aliased to the | ||||||||||||||||||||||||||||
| 208 | # dispatcher. | ||||||||||||||||||||||||||||
| 209 | 6 | ▶ | if(eval { no strict 'refs'; defined &{"$multi_package\::$subname"} }) { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 0 | die "Cannot redefine $subname in $multi_package"; | |||||||||||||||||||||||||||
| 211 | } else { | ||||||||||||||||||||||||||||
| 212 | 5 | ▶ | _hlog { "Making $multi_package\::$subname stub" } 2; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 213 | 6 | ▶ | do { no strict 'refs'; *{"$multi_package\::$subname"} = sub {} }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | } #import() | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | # Parse the argument list to the attribute handler | ||||||||||||||||||||||||||||
| 218 | 8 | ▶ | sub _parse_arglist { | ||||||||||||||||||||||||||
|
Subroutine
_parse_arglist: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 219 | 8 | my ($spec, $funcname) = @_; | |||||||||||||||||||||||||||
| 220 | 8 | ▶ | _croak "Need a parameter spec for $funcname" unless $spec; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 221 | 8 | ▶ | _hlog { "Parsing args for $funcname: $spec" } 2; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 8 | return Sub::Multi::Tiny::SigParse::Parse($spec); | |||||||||||||||||||||||||||
| 224 | } #_parse_arglist | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | # Create the source for the M attribute handler for a given package | ||||||||||||||||||||||||||||
| 227 | 5 | ▶ | sub _make_M { | ||||||||||||||||||||||||||
|
Subroutine
_make_M: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 228 | 5 | my $multi_package = shift; | |||||||||||||||||||||||||||
| 229 | 5 | my $P = __PACKAGE__; | |||||||||||||||||||||||||||
| 230 | 5 | my $code = _line_mark_string | |||||||||||||||||||||||||||
| 231 | "package $multi_package;\n"; | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | # TODO See if making M an :ATTR(..., BEGIN) permits us to remove the | ||||||||||||||||||||||||||||
| 234 | # requirement to list all the parameters in the `use S::M::T` line | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 5 | $code .= _line_mark_string <<'EOT'; | |||||||||||||||||||||||||||
| 237 | 5 | ▶ | use Attribute::Handlers; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 238 | 5 | ▶ | use Sub::Multi::Tiny::Util qw(_hlog); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 239 | ##use Data::Dumper; | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | sub M :ATTR(CODE,RAWDATA) { | ||||||||||||||||||||||||||||
| 242 | _hlog { require Data::Dumper; | ||||||||||||||||||||||||||||
| 243 | 'In ', __PACKAGE__, "::M: \n", | ||||||||||||||||||||||||||||
| 244 | Data::Dumper->Dump([\@_], ['attr_args']) } 2; | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | my ($package, $symbol, $referent, $attr, $data, $phase, | ||||||||||||||||||||||||||||
| 247 | $filename, $linenum) = @_; | ||||||||||||||||||||||||||||
| 248 | my $funcname = "$package\::" . *{$symbol}{NAME}; | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | _hlog { # Code from Attribute::Handlers, license perl_5 | ||||||||||||||||||||||||||||
| 251 | ref($referent), | ||||||||||||||||||||||||||||
| 252 | $funcname, | ||||||||||||||||||||||||||||
| 253 | "($referent)", "was just declared", | ||||||||||||||||||||||||||||
| 254 | "and ascribed the ${attr} attribute", | ||||||||||||||||||||||||||||
| 255 | "with data ($data)", | ||||||||||||||||||||||||||||
| 256 | "in phase $phase", | ||||||||||||||||||||||||||||
| 257 | "in file $filename at line $linenum" | ||||||||||||||||||||||||||||
| 258 | } 2; | ||||||||||||||||||||||||||||
| 259 | EOT | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | # Trap out-of-sequence calls. Currently you can't create a new multisub | ||||||||||||||||||||||||||||
| 262 | # via eval at runtime. TODO use UNITCHECK instead to permit doing so? | ||||||||||||||||||||||||||||
| 263 | 5 | $code .= _line_mark_string <<EOT; | |||||||||||||||||||||||||||
| 264 | die 'Dispatchers already created - please file a bug report' | ||||||||||||||||||||||||||||
| 265 | if $P\::_dispatchers_created(); | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | my \$multi_def = \$_multisubs{'$multi_package'}; | ||||||||||||||||||||||||||||
| 268 | EOT | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | # Parse and validate the args | ||||||||||||||||||||||||||||
| 271 | 5 | $code .= _line_mark_string <<EOT; | |||||||||||||||||||||||||||
| 272 | my \$hrSig = $P\::_parse_arglist(\$data, \$funcname); | ||||||||||||||||||||||||||||
| 273 | $P\::_check_and_inflate_sig(\$hrSig, \$multi_def, | ||||||||||||||||||||||||||||
| 274 | \$funcname, \$package, \$filename, \$linenum); | ||||||||||||||||||||||||||||
| 275 | EOT | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | 5 | $code .= _line_mark_string <<'EOT'; | |||||||||||||||||||||||||||
| 278 | EOT | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | # Save the implementation's info for use when making the dispatcher. | ||||||||||||||||||||||||||||
| 281 | 5 | $code .= _line_mark_string <<'EOT'; | |||||||||||||||||||||||||||
| 282 | my $info = { | ||||||||||||||||||||||||||||
| 283 | code => $referent, | ||||||||||||||||||||||||||||
| 284 | args => $hrSig->{parms}, # TODO remove eventually | ||||||||||||||||||||||||||||
| 285 | sig => $hrSig, | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | # For error messages | ||||||||||||||||||||||||||||
| 288 | filename => $filename, | ||||||||||||||||||||||||||||
| 289 | linenum => $linenum, | ||||||||||||||||||||||||||||
| 290 | candidate_name => $funcname | ||||||||||||||||||||||||||||
| 291 | }; | ||||||||||||||||||||||||||||
| 292 | push @{$multi_def->{impls}}, $info; | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | 5 | ▶ | } #M | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 295 | EOT | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | 5 | ▶ | _hlog { "M code:\n$code\n" } 2; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 298 | 5 | return $code; | |||||||||||||||||||||||||||
| 299 | } #_make_M | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | # Validate a signature and convert text to usable objects | ||||||||||||||||||||||||||||
| 302 | 10 | ▶ | sub _check_and_inflate_sig { | ||||||||||||||||||||||||||
|
Subroutine
_check_and_inflate_sig: called CC=11 SCAR=25.2
| |||||||||||||||||||||||||||||
| 303 | 16 | my ($signature, $multi_def, $funcname, $package, $filename, $linenum) = @_; | |||||||||||||||||||||||||||
| 304 | 16 | my ($saw_positional, $saw_named); | |||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | 16 | my $args = $signature->{parms}; | |||||||||||||||||||||||||||
| 307 | 16 | my $temp; | |||||||||||||||||||||||||||
| 308 | 16 | foreach (@$args) { | |||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | # Is the argument valid in this package? | ||||||||||||||||||||||||||||
| 311 | 17 | my $name = $_->{name}; | |||||||||||||||||||||||||||
| 312 | 17 | ▶ | unless($multi_def->{possible_params}->{$name}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 313 | 9 | die "Argument $name is not listed on the 'use Sub::Multi::Tiny' line (used by $funcname at $filename\:$linenum"; | |||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | # Is the argument out of order? | ||||||||||||||||||||||||||||
| 317 | die "Positional arguments must precede named arguments" | ||||||||||||||||||||||||||||
| 318 | 15 | ▶ | if $saw_named && !$_->{named}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$saw_named and not $_->{"named"}
MC/DC0%0/2
$saw_named and not $_->{"named"}
$saw_named
not $_->{"named"}
Truth table33%1/3
A and not(B)
A= $saw_namedB= not $_->{"named"}
| |||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | # Inflate type constraint, if any | ||||||||||||||||||||||||||||
| 321 | 15 | ▶ | if($_->{type}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 322 | 0 | ▶ | _hlog { In => $package, "evaluating type '$_->{type}'" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 323 | 3 | $temp = eval _line_mark_string <<EOT; | |||||||||||||||||||||||||||
| 324 | do { | ||||||||||||||||||||||||||||
| 325 | package $package; | ||||||||||||||||||||||||||||
| 326 | $_->{type} # Anything meaningful in the calling package is OK | ||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||
| 328 | EOT | ||||||||||||||||||||||||||||
| 329 | 3 | ▶ | die "In $package: Could not understand type '$_->{type}': $@" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 330 | 3 | $_->{type} = $temp; | |||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | # Inflate where clause, if any, into a closure | ||||||||||||||||||||||||||||
| 334 | 8 | ▶ | if($_->{where}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 335 | 2 | ▶ | _hlog { In => $package, "evaluating 'where' clause '$_->{where}'" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 336 | 2 | $temp = eval _line_mark_string <<EOT; | |||||||||||||||||||||||||||
| 337 | do { | ||||||||||||||||||||||||||||
| 338 | package $package; | ||||||||||||||||||||||||||||
| 339 | sub $_->{where} # Anything meaningful in the calling package is OK | ||||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||
| 341 | EOT | ||||||||||||||||||||||||||||
| 342 | 2 | ▶ | die "In $package: Could not understand 'where' clause '$_->{where}': $@" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 343 | 2 | $_->{where} = $temp; | |||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | # Remember data for later | ||||||||||||||||||||||||||||
| 347 | 8 | ▶ | $saw_named ||= $_->{named}; | ||||||||||||||||||||||||||
|
Condition33%1/3
$saw_named ||= $_->{"named"}
MC/DC0%0/2
$saw_named ||= $_->{"named"}
$saw_named
$_->{"named"}
Truth table33%1/3
A ||= B
A= $saw_namedB= $_->{"named"}
| |||||||||||||||||||||||||||||
| 348 | 8 | ▶ | $saw_positional ||= !$_->{named}; | ||||||||||||||||||||||||||
|
Condition33%1/3
$saw_positional ||= !$_->{"named"}
MC/DC0%0/2
$saw_positional ||= !$_->{"named"}
$saw_positional
!$_->{"named"}
Truth table33%1/3
A ||= not(B)
A= $saw_positionalB= !$_->{"named"}
| |||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||
| 351 | } # _check_and_inflate_sig | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | # Create a dispatcher | ||||||||||||||||||||||||||||
| 354 | 4 | ▶ | sub _make_dispatcher { | ||||||||||||||||||||||||||
|
Subroutine
_make_dispatcher: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 355 | 4 | my $hr = shift; | |||||||||||||||||||||||||||
| 356 | die "No implementations given for $hr->{defined_in}" | ||||||||||||||||||||||||||||
| 357 | 4 | ▶ | unless @{$hr->{impls}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | 4 | my $custom_dispatcher = do { | |||||||||||||||||||||||||||
| 360 | 6 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 361 | 4 | *{ $hr->{defined_in} . '::MakeDispatcher' }{CODE} | |||||||||||||||||||||||||||
| 362 | 4 | }; | |||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | 4 | ▶ | return $custom_dispatcher->($hr) if defined $custom_dispatcher; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | # Default dispatcher | ||||||||||||||||||||||||||||
| 367 | 3 | require Sub::Multi::Tiny::Dispatcher::Default; | |||||||||||||||||||||||||||
| 368 | 3 | return Sub::Multi::Tiny::Dispatcher::Default::MakeDispatcher($hr); | |||||||||||||||||||||||||||
| 369 | } #_make_dispatcher | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | 6 | 1; | |||||||||||||||||||||||||||
| 372 | # Rest of the documentation {{{1 | ||||||||||||||||||||||||||||
| 373 | __END__ | ||||||||||||||||||||||||||||