| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 34 | 3539 | ▶ | next unless Scalar::Util::blessed $sv; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 113 | ▶ | next unless $sv->can($method); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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)
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)
| |||||||||||||||||||||||||||||
| 44 | 2 | ▶ | if( $keeper_type ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
*{$package . $k;}{"CODE"} and $k eq $given_method
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 69 | 5967 | ▶ | next if $k =~ m/[^\w:]/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 5771 | ▶ | next unless $k =~ m/::$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | 282 | return; # backtrack/failure | |||||||||||||||||||||||||||
| 79 | 1 | }->('main::'); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 2 | 1; | |||||||||||||||||||||||||||
| 85 | __END__ | ||||||||||||||||||||||||||||