| 1 | ######################################################################## | ||||||||||||||||||||
| 2 | # housekeeping | ||||||||||||||||||||
| 3 | ######################################################################## | ||||||||||||||||||||
| 4 | 9 | ▶ | use v5.24; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | package mro::EVERY v1.0.3; | ||||||||||||||||||||
| 7 | 9 | ▶ | use mro; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 9 | ▶ | use Carp qw( croak ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 9 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 9 | ▶ | use Symbol qw( qualify_to_ref ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | ######################################################################## | ||||||||||||||||||||
| 14 | # package varaibles | ||||||||||||||||||||
| 15 | ######################################################################## | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 9 | our @CARP_NOT = ( __PACKAGE__, qw( mro ) ); | |||||||||||||||||||
| 18 | 9 | my %class2dfs = (); | |||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | ######################################################################## | ||||||||||||||||||||
| 21 | # utility subs | ||||||||||||||||||||
| 22 | ######################################################################## | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | my $find_name | ||||||||||||||||||||
| 25 | = sub | ||||||||||||||||||||
| 26 | 24 | ▶ | { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=19.7
| |||||||||||||||||||||
| 27 | 24 | my $proto = shift; | |||||||||||||||||||
| 28 | 24 | my $auto = shift; | |||||||||||||||||||
| 29 | 24 | my ($name) = $auto =~ m{ (\w+) $}x; | |||||||||||||||||||
| 30 | 24 | ▶ | my $class = blessed $proto || $proto; | ||||||||||||||||||
|
Condition33%1/3
builtin::blessed($proto) || $proto
MC/DC0%0/2
builtin::blessed($proto) || $proto
builtin::blessed($proto)
$proto
Truth table33%1/3
A || B
A= builtin::blessed($proto)B= $proto
| |||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 24 | ▶ | $proto->can( $name ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 33 | or croak "Botched EVERY: '$proto' cannot '$name'"; | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | # class at entry point gets to decide the mro type. | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 24 | ▶ | my $mro = $class2dfs{ $class } || $class->mro::get_mro; | ||||||||||||||||||
|
Condition33%1/3
$class2dfs{$class} || $class->mro::get_mro
MC/DC0%0/2
$class2dfs{$class} || $class->mro::get_mro
$class2dfs{$class}
$class->mro::get_mro
Truth table33%1/3
A || B
A= $class2dfs{$class}B= $class->mro::get_mro
| |||||||||||||||||||||
| 38 | 24 | my @isa = $class->mro::get_linear_isa( $mro )->@*; | |||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | # @found preserves array context of map. | ||||||||||||||||||||
| 41 | # | ||||||||||||||||||||
| 42 | # this should never croak afer the can | ||||||||||||||||||||
| 43 | # check unless they have an overloaded | ||||||||||||||||||||
| 44 | # can and forgot qw( autoload ); | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | my @found | ||||||||||||||||||||
| 47 | = map | ||||||||||||||||||||
| 48 | { | ||||||||||||||||||||
| 49 | 24 | ▶ | *{ qualify_to_ref $name => $_ }{ CODE } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 50 | or | ||||||||||||||||||||
| 51 | () | ||||||||||||||||||||
| 52 | 48 | ▶ | } | ||||||||||||||||||
|
Condition33%1/3
*{&qualify_to_ref($name, $_);}{'CODE'} || ()
MC/DC0%0/2
*{&qualify_to_ref($name, $_);}{'CODE'} || ()
*{&qualify_to_ref($name, $_);}{'CODE'}
()
Truth table33%1/3
A || B
A= *{&qualify_to_ref($name, $_);}{'CODE'}B= ()
| |||||||||||||||||||||
| 53 | @isa | ||||||||||||||||||||
| 54 | or | ||||||||||||||||||||
| 55 | croak "Bogus $proto: '$name' not in @isa"; | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | @found | ||||||||||||||||||||
| 58 | 24 | }; | |||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | my $find_auto | ||||||||||||||||||||
| 61 | = sub | ||||||||||||||||||||
| 62 | 28 | ▶ | { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.1
| |||||||||||||||||||||
| 63 | 28 | my $proto = shift; | |||||||||||||||||||
| 64 | 28 | my $auto = shift; | |||||||||||||||||||
| 65 | 28 | my ($name) = $auto =~ m{ (\w+) $}x; | |||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 28 | ▶ | $proto->can( $name ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 68 | or croak "Botched EVERY: '$proto' cannot '$name'"; | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 28 | local $" = ','; | |||||||||||||||||||
| 71 | 28 | my @isa = $proto->mro::get_linear_isa->@*; | |||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | # @found preserves array context of map. | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | my @found | ||||||||||||||||||||
| 76 | = grep | ||||||||||||||||||||
| 77 | { | ||||||||||||||||||||
| 78 | 60 | $_ | |||||||||||||||||||
| 79 | } | ||||||||||||||||||||
| 80 | map | ||||||||||||||||||||
| 81 | { | ||||||||||||||||||||
| 82 | 28 | ▶ | *{ qualify_to_ref $name => $_ }{ CODE } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 83 | or | ||||||||||||||||||||
| 84 | do | ||||||||||||||||||||
| 85 | 60 | ▶ | { | ||||||||||||||||||
|
Condition66%2/3
*{&qualify_to_ref($name, $_);}{'CODE'} || do {
my $isa = &qualify_to_ref('ISA', $_);
my $ref = &qualify_to_ref('AUTOLOAD', $_);
local *$isa = [];
my $al = $_->can($name) ? *{$ref;}{'CODE'} : '';
$al ? sub {
local *{$ref;} = $auto;
goto \&$al;
}
: ()
}
MC/DC0%0/2
*{&qualify_to_ref($name, $_);}{'CODE'} || do {
my $isa = &qualify_to_ref('ISA', $_);
my $ref = &qualify_to_ref('AUTOLOAD', $_);
local *$isa = [];
my $al = $_->can($name) ? *{$ref;}{'CODE'} : '';
$al ? sub {
local *{$ref;} = $auto;
goto \&$al;
}
: ()
}
*{&qualify_to_ref($name, $_);}{'CODE'}
do {
my $isa = &qualify_to_ref('ISA', $_);
my $ref = &qualify_to_ref('AUTOLOAD', $_);
local *$isa = [];
my $al = $_->can($name) ? *{$ref;}{'CODE'} : '';
$al ? sub {
local *{$ref;} = $auto;
goto \&$al;
}
: ()
}
Truth table66%2/3
A || B
A= *{&qualify_to_ref($name, $_);}{'CODE'}B= do {
my $isa = &qualify_to_ref('ISA', $_);
my $ref = &qualify_to_ref('AUTOLOAD', $_);
local *$isa = [];
my $al = $_->can($name) ? *{$ref;}{'CODE'} : '';
$al ? sub {
local *{$ref;} = $auto;
goto \&$al;
}
: ()
}
| |||||||||||||||||||||
| 86 | 36 | my $isa = qualify_to_ref ISA => $_; | |||||||||||||||||||
| 87 | 36 | my $ref = qualify_to_ref AUTOLOAD => $_; | |||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 36 | local *$isa = []; | |||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | # at this point can is isolated to the | ||||||||||||||||||||
| 92 | # single pacakge. | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | my $al | ||||||||||||||||||||
| 95 | = $_->can( $name ) | ||||||||||||||||||||
| 96 | 36 | ? *{ $ref }{ CODE } | |||||||||||||||||||
| 97 | 36 | ▶ | : '' | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | ; | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | $al | ||||||||||||||||||||
| 101 | ? sub | ||||||||||||||||||||
| 102 | 36 | ▶ | { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 103 | # at this point if package can $name and | ||||||||||||||||||||
| 104 | # has an AUTOLOAD but not the named sub. | ||||||||||||||||||||
| 105 | # | ||||||||||||||||||||
| 106 | # install $AUTOLOAD and bon voyage! | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 36 | local *{ $ref } = $auto; | |||||||||||||||||||
| 109 | 36 | goto &$al; | |||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | : () | ||||||||||||||||||||
| 112 | 36 | ▶ | ; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | } | ||||||||||||||||||||
| 115 | @isa | ||||||||||||||||||||
| 116 | or | ||||||||||||||||||||
| 117 | croak "Bogus $proto: '$name' & AUTOLOAD not in @isa"; | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | @found | ||||||||||||||||||||
| 120 | 28 | }; | |||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | 9 | my $finder = $find_name; | |||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | sub import | ||||||||||||||||||||
| 125 | 9 | ▶ | { | ||||||||||||||||||
|
Subroutine
import: called CC=6 SCAR=22.9
| |||||||||||||||||||||
| 126 | 9 | shift; | |||||||||||||||||||
| 127 | 9 | my $caller = caller; | |||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | 9 | for( @_ ) | |||||||||||||||||||
| 130 | { | ||||||||||||||||||||
| 131 | 4 | my ( $status, $arg ) = m{ (no)? (dfs|autoload) }x; | |||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 4 | ▶ | if( $arg eq 'dfs' ) | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 134 | { | ||||||||||||||||||||
| 135 | # delay the lookup of mro::get_mro until runtim | ||||||||||||||||||||
| 136 | # to allow classes to fiddle with it at runtime. | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | 0 | ▶ | if( $status ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 139 | { | ||||||||||||||||||||
| 140 | 0 | delete $class2dfs{ $caller } | |||||||||||||||||||
| 141 | } | ||||||||||||||||||||
| 142 | else | ||||||||||||||||||||
| 143 | { | ||||||||||||||||||||
| 144 | 0 | $class2dfs{ $caller } = 'dfs' | |||||||||||||||||||
| 145 | } | ||||||||||||||||||||
| 146 | } | ||||||||||||||||||||
| 147 | elsif( $arg eq 'autoload' ) | ||||||||||||||||||||
| 148 | { | ||||||||||||||||||||
| 149 | 4 | ▶ | $finder | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 150 | = $status | ||||||||||||||||||||
| 151 | ? $find_name | ||||||||||||||||||||
| 152 | : $find_auto | ||||||||||||||||||||
| 153 | } | ||||||||||||||||||||
| 154 | else | ||||||||||||||||||||
| 155 | { | ||||||||||||||||||||
| 156 | 0 | croak "Botched EVERY: unknown argument '$_'"; | |||||||||||||||||||
| 157 | } | ||||||||||||||||||||
| 158 | } | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | return | ||||||||||||||||||||
| 161 | 9 | } | |||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | ######################################################################## | ||||||||||||||||||||
| 164 | # downstream packages | ||||||||||||||||||||
| 165 | ######################################################################## | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | package EVERY; | ||||||||||||||||||||
| 168 | 9 | ▶ | use Carp qw( croak ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 169 | 9 | ▶ | use List::Util qw( uniq ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | 9 | our @CARP_NOT = ( __PACKAGE__, qw( mro ) ); | |||||||||||||||||||
| 172 | 9 | our $AUTOLOAD = ''; | |||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | AUTOLOAD | ||||||||||||||||||||
| 175 | 26 | ▶ | { | ||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=3 SCAR=11.4
| |||||||||||||||||||||
| 176 | 26 | ▶ | my $proto = shift | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 177 | or croak "Bogus EVERY, called without an object."; | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | # remaining arguments left on the stack. | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | $proto->$_( @_ ) | ||||||||||||||||||||
| 182 | 26 | for uniq $proto->$finder( $AUTOLOAD ); | |||||||||||||||||||
| 183 | } | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | package EVERY::LAST; | ||||||||||||||||||||
| 186 | 9 | ▶ | use Carp qw( croak ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 187 | 9 | ▶ | use List::Util qw( uniq ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | 9 | our @CARP_NOT = ( __PACKAGE__, qw( mro ) ); | |||||||||||||||||||
| 190 | 9 | our $AUTOLOAD = ''; | |||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | AUTOLOAD | ||||||||||||||||||||
| 193 | 26 | ▶ | { | ||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=3 SCAR=11.4
| |||||||||||||||||||||
| 194 | 26 | ▶ | my $proto = shift | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 195 | or croak "Bogus EVERY::LAST, called without an object."; | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | # remaining arguments left on the stack. | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | $proto->$_( @_ ) | ||||||||||||||||||||
| 200 | 26 | for uniq reverse $proto->$finder( $AUTOLOAD ); | |||||||||||||||||||
| 201 | } | ||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | # keep require happy | ||||||||||||||||||||
| 204 | 9 | 1 | |||||||||||||||||||
| 205 | __END__ | ||||||||||||||||||||