blib/lib/autobox/Bless.pm

filter: Statementstmt 98.5% 70 / 71 Branchbran 90.9% 20 / 22 Conditioncond 83.3% 5 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 15 / 15 Podpod n/a totaltotal 94.9% 112 / 118 CC 18 SCAR 18.1
CC
18
Cov
96%
AUTOLOAD
10.2
__ANON__
9.0
BEGIN
1.0
CRAP 6.1
↑ Summary
1 package autobox::Bless;
2  
3 2 ▶use 5.010000;
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 our $VERSION = '0.02';
8  
9 2 ▶use autobox;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use base 'autobox';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Devel::Gladiator;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Devel::Peek;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Devel::Caller 'caller_cv'; # cx_type is 40 not CXt_SUB unless it's the current version
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # use PadWalker;
19 # use B;
20  
21 # could take one of three approaches; remember every field seen in every class; remember the top n closest matches as we go; take the first good match
22 # it's a memory vs accuracy tradeoff
23 # could also take a hybrid approach and if we don't find an exact match, look for a best match
24  
25 2 ▶sub HASH::AUTOLOAD {
Subroutine AUTOLOAD: called CC=10 SCAR=23.2
26 2 my $unblessed_hash = shift;
27 2 ▶ return if $HASH::AUTOLOAD =~ m/::DESTROY$/;
Branch: 1 branch
BranchTrueFalseTotal
if $HASH::AUTOLOAD =~ /::DESTROY$/ 0 2 2
28 2 (my $method) = $HASH::AUTOLOAD =~ m/.*::(.*)/;
29 # warn "``$method'' called";
30 # my @contenders; # ( [ package, score ], ... )
31 2 my $keeper_type;
32 2 for my $sv ( @{ Devel::Gladiator::walk_arena() } ) {
33 165537 ▶ next unless UNIVERSAL::isa($sv, 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($sv, "HASH") 161998 3539 165537
34 3539 ▶ next unless Scalar::Util::blessed $sv;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($sv) 3426 113 3539
35 113 ▶ next unless $sv->can($method);
Branch: 1 branch
BranchTrueFalseTotal
unless $sv->can($method) 112 1 113
36 # warn "considering type " . Scalar::Util::blessed $sv;
37 1 for my $field ( %{ $unblessed_hash } ) {
38 4 ▶ exists $sv->{$field} or next;
Branch: 1 branch
BranchTrueFalseTotal
exists $sv->{$field} or next 2 2 4
39 }
40 # use Devel::ArgNames; my @argnames = Devel::ArgNames::arg_names(@_ XXX before the shift); my $type = ref $sv; bless peek_my(0)->{'%'.$argnames[0]}, $type;
41 1 $keeper_type = Scalar::Util::blessed $sv;
42 }
43 2 ▶ $keeper_type ||= autobox::Bless::_package_with_method($method); # backup plan
Condition66%2/3
$keeper_type ||= _package_with_method($method)
l!l&&r!l&&!r
110
MC/DC0%0/2
$keeper_type ||= _package_with_method($method)
$keeper_type _package_with_method($method)
Truth table66%2/3
A ||= B
A= $keeper_typeB= _package_with_method($method)
ABresult
1X1
011
000
44 2 ▶ if( $keeper_type ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keeper_type) { } 2 0 2
45 # warn "won with type " . $keeper_type;
46 # $keeper_type->can($method)->($unblessed_hash, @_); # or even better:
47 2 bless $unblessed_hash, $keeper_type; $unblessed_hash->$method(@_);
48 } else {
49 0 Carp::confess qq{Can't call method "$method" without a package or object reference, and believe me, I tried};
50 }
51 }
52  
53 1 ▶sub _package_with_method {
Subroutine _package_with_method: called CC=1 SCAR=0.0
54 # look through the package hierarchy looking for something with the given method (er, function)
55 1 my $given_method = shift;
56 284 ▶ sub {
Subroutine __ANON__: called CC=9 SCAR=22.0
57 284 my $package = shift;
58 # warn "considering package ``$package''";
59 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
60 284 for my $k (keys %$package) {
61 5986 ▶ if(*{$package.$k}{CODE} and $k eq $given_method) {
Branch: 1 branch
BranchTrueFalseTotal
if (*{$package . $k;}{"CODE"} and $k eq $given_method) 1 5985 5986
Condition100%3/3
*{$package . $k;}{"CODE"} and $k eq $given_method
!ll&&!rl&&r
147845071
MC/DC100%2/2
*{$package . $k;}{"CODE"} and $k eq $given_method
*{$package . $k;}{"CODE"} $k eq $given_method
Truth table100%3/3
A and B
A= *{$package . $k;}{"CODE"}B= $k eq $given_method
ABresult
0X0
100
111
62 # warn "found it!";
63 1 $package =~ s{::$}{};
64 1 return $package; # success!
65 }
66 }
67 283 for my $k (keys %$package) {
68 5968 ▶ next if $k =~ m/main::$/;
Branch: 1 branch
BranchTrueFalseTotal
if $k =~ /main::$/ 1 5967 5968
69 5967 ▶ next if $k =~ m/[^\w:]/;
Branch: 1 branch
BranchTrueFalseTotal
if $k =~ /[^\w:]/ 196 5771 5967
70 5771 ▶ next unless $k =~ m/::$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $k =~ /::$/ 5488 283 5771
71 # recurse into that namespace unless it corresponds to a .pm module that got used at some point
72 283 my $modulepath = $package.$k;
73 # for($modulepath) { s{^main::}{}; s{::$}{}; s{::}{/}g; $_ .= '.pm'; }
74 # next if exists $INC{$modulepath};
75 283 my $maybe_result = caller_cv(0)->($package.$k); # press on forward into darker depths
76 283 ▶ return $maybe_result if $maybe_result;
Branch: 1 branch
BranchTrueFalseTotal
if $maybe_result 1 282 283
77 }
78 282 return; # backtrack/failure
79 1 }->('main::');
80 }
81  
82  
83  
84 2 1;
85 __END__
↑ Summary