/usr/share/perl5/Kavorka/Multi.pm is in libkavorka-perl 0.036-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | use 5.014;
use strict;
use warnings;
use Sub::Util ();
package Kavorka::Multi;
our $AUTHORITY = 'cpan:TOBYINK';
our $VERSION = '0.036';
use Parse::Keyword {};
use Parse::KeywordX;
use Moo;
with 'Kavorka::Sub';
use namespace::sweep;
has multi_type => (is => 'ro', required => 1);
has declared_long_name => (is => 'rwp');
has qualified_long_name => (is => 'rwp');
around parse => sub
{
my $next = shift;
my $class = shift;
lex_read_space;
my $type = parse_name('keyword', 0);
lex_read_space;
$class->multi_parse($next, $type, @_);
};
sub multi_parse
{
my $class = shift;
my ($parse_method, $keyword, @args) = @_;
my $implementation;
if ($^H{Kavorka} =~ /\b$keyword=(\S+)/)
{
$implementation = $1;
}
else
{
Carp::croak("Could not resolve keyword '$keyword'");
}
return $class->$parse_method(@args, multi_type => $implementation);
}
after parse_attributes => sub
{
my $self = shift;
my @attr = @{$self->attributes};
my @filtered;
$_->[0] eq 'long'
? ($self->_set_declared_long_name($_->[1]), $self->_set_qualified_long_name(scalar Kavorka::_fqname $_->[1]))
: push(@filtered, $_)
for @attr;
@{$self->attributes} = @filtered;
};
after parse_signature => sub
{
my $self = shift;
my $sig = $self->signature;
for my $param (@{$sig->params})
{
Carp::croak("Type constraints for parameters cannot be 'assumed' in a multi sub")
if $param->traits->{assumed};
}
$self->signature->_set_nobble_checks(1);
};
sub allow_anonymous { 0 }
sub allow_lexical { 0 }
sub default_attributes
{
my $code = $_[0]->multi_type->can('default_attributes');
goto $code;
}
sub default_invocant
{
my $code = $_[0]->multi_type->can('default_invocant');
goto $code;
}
sub forward_declare
{
my $code = $_[0]->multi_type->can('forward_declare');
goto $code;
}
sub invocation_style
{
$_[0]->multi_type->invocation_style
or Carp::croak("No invocation style defined");
}
our %DISPATCH_TABLE;
our %DISPATCH_STYLE;
our %INVALIDATION;
sub __gather_candidates
{
my ($pkg, $subname, $args) = @_;
if ($DISPATCH_STYLE{$pkg}{$subname} eq 'fun')
{
return @{$DISPATCH_TABLE{$pkg}{$subname}};
}
require mro;
my $invocant = ref($args->[0]) || $args->[0];
return map @{$DISPATCH_TABLE{$_}{$subname} || [] }, @{ $invocant->mro::get_linear_isa };
}
sub __dispatch
{
my ($pkg, $subname) = @{ +shift };
for my $c ( __gather_candidates($pkg, $subname, \@_) )
{
my @copy = @_;
next unless $c->signature->check(@copy);
my $body = $c->body;
goto $body;
}
Carp::croak("Arguments to $pkg\::$subname did not match any known signature for multi sub");
}
sub __compile
{
my ($pkg, $subname) = @_;
my @candidates = __gather_candidates($pkg, $subname, [$pkg]);
my @coderefs = map $_->body, @candidates;
my $slowpath = '';
if ($DISPATCH_STYLE{$pkg}{$subname} ne 'fun')
{
my $this = [$pkg, $subname];
push @{ $INVALIDATION{"$_\::$subname"} ||= [] }, $this for @{ $pkg->mro::get_linear_isa };
$slowpath = sprintf(
'if ((ref($_[0]) || $_[0]) ne %s) { unshift @_, [%s, %s]; goto \\&Kavorka::Multi::__dispatch }',
B::perlstring($pkg),
B::perlstring($pkg),
B::perlstring($subname),
);
}
my $compiled = join q[] => (
map {
my $sig = $candidates[$_]->signature;
$sig && $sig->nobble_checks ? sprintf(
"\@tmp = \@_; if (%s) { unshift \@_, \$Kavorka::Signature::NOBBLE; goto \$coderefs[%d] }\n",
$candidates[$_]->signature->inline_check('@tmp'),
$_,
) :
$sig ? sprintf(
"\@tmp = \@_; if (%s) { goto \$coderefs[%d] }\n",
$candidates[$_]->signature->inline_check('@tmp'),
$_,
) : sprintf('goto \$coderefs[%d];', $_);
} 0 .. $#candidates,
);
my $error = "Carp::croak(qq/Arguments to $pkg\::$subname did not match any known signature for multi sub/);";
Sub::Util::set_subname(
"$pkg\::$subname",
eval("package $pkg; sub { $slowpath; my \@tmp; $compiled; $error }"),
);
}
sub __defer_compile
{
my ($pkg, $subname) = @_;
return Sub::Util::set_subname(
"$pkg\::$subname" => sub {
no strict "refs";
no warnings "redefine";
*{"$pkg\::$subname"} = (my $compiled = __compile($pkg, $subname));
goto $compiled;
},
);
}
sub install_sub
{
my $self = shift;
my ($pkg, $subname) = ($self->qualified_name =~ /^(.+)::(\w+)$/);
unless ($DISPATCH_TABLE{$pkg}{$subname})
{
$DISPATCH_TABLE{$pkg}{$subname} = [];
$DISPATCH_STYLE{$pkg}{$subname} = $self->invocation_style;
}
$DISPATCH_STYLE{$pkg}{$subname} eq $self->invocation_style
or Carp::croak("Two different invocation styles used for $subname");
{
# A placeholder dispatcher that will replace itself with a more
# efficient optimized (compiled) dispatcher.
no strict "refs";
no warnings "redefine";
*{"$pkg\::$subname"} = __defer_compile($pkg, $subname);
# Invalidate previously optimized dispatchers in subclasses of $pkg
*{join '::', @$_} = __defer_compile(@$_)
for @{ delete($INVALIDATION{"$pkg\::$subname"}) || [] };
}
my $long = $self->qualified_long_name;
if (defined $long)
{
no strict 'refs';
*$long = $self->body;
}
push @{ $DISPATCH_TABLE{$pkg}{$subname} }, $self;
}
1;
|