| 1 | package MRO::Magic; | ||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||
| 3 | 7 | $MRO::Magic::VERSION = '0.100001'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use 5.010; # uvar magic does not work prior to version 10 | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | # ABSTRACT: write your own method dispatcher | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 7 | ▶ | use mro; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 7 | ▶ | use MRO::Define; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 7 | ▶ | use Scalar::Util qw(reftype); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 7 | ▶ | use Variable::Magic qw/wizard cast/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | sub import { | ||||||||||||||||||||||||||||
| 17 | 8 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=13 SCAR=25.7
| |||||||||||||||||||||||||||||
| 18 | 8 | my $arg; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 8 | ▶ | if (@_ == 1 and reftype $_[0] eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ == 1 and builtin::reftype($_[0]) eq "CODE"
MC/DC50%1/2
@_ == 1 and builtin::reftype($_[0]) eq "CODE"
@_ == 1
builtin::reftype($_[0]) eq "CODE"
Truth table66%2/3
A and B
A= @_ == 1B= builtin::reftype($_[0]) eq "CODE"
| |||||||||||||||||||||||||||||
| 21 | 2 | $arg = { metamethod => $_[0] }; | |||||||||||||||||||||||||||
| 22 | } else { | ||||||||||||||||||||||||||||
| 23 | 6 | $arg = { @_ }; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 8 | my $caller = caller; | |||||||||||||||||||||||||||
| 27 | 8 | my %to_install; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 8 | my $code = $arg->{metamethod}; | |||||||||||||||||||||||||||
| 30 | 8 | ▶ | my $metamethod = $arg->{metamethod_name} || '__metamethod__'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$arg->{'metamethod_name'} || '__metamethod__'
MC/DC100%1/1
$arg->{'metamethod_name'} || '__metamethod__'
$arg->{'metamethod_name'}
Truth table100%2/2
A
A= $arg->{'metamethod_name'}
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 8 | ▶ | if (reftype $code eq 'SCALAR') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 4 | ▶ | Carp::confess("can't find metamethod via name ${ $arg->{metamethod} }") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | unless $code = $caller->can($$code); | ||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 7 | ▶ | if (do { no strict 'refs'; defined *{"$caller\::$metamethod"}{CODE} }) { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 1 | Carp::confess("can't install metamethod as $metamethod; already defined"); | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 6 | my $method_name; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | my $wiz = wizard | ||||||||||||||||||||||||||||
| 44 | copy_key => 1, | ||||||||||||||||||||||||||||
| 45 | 6 | ▶ | data => sub { \$method_name }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | fetch => $self->_gen_fetch_magic({ | ||||||||||||||||||||||||||||
| 47 | metamethod => $metamethod, | ||||||||||||||||||||||||||||
| 48 | passthru => $arg->{passthru}, | ||||||||||||||||||||||||||||
| 49 | 6 | }); | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | $to_install{ $metamethod } = sub { | ||||||||||||||||||||||||||||
| 52 | 105 | ▶ | my $invocant = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 53 | 105 | $code->($invocant, $method_name, \@_); | |||||||||||||||||||||||||||
| 54 | 6 | }; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 7 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 57 | 6 | for my $key (keys %to_install) { | |||||||||||||||||||||||||||
| 58 | 6 | *{"$caller\::$key"} = $to_install{ $key }; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 6 | ▶ | if ($arg->{overload}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 1 | my %copy = %{ $arg->{overload} }; | |||||||||||||||||||||||||||
| 63 | 1 | for my $ol (keys %copy) { | |||||||||||||||||||||||||||
| 64 | 2 | ▶ | next if $ol eq 'fallback'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 1 | ▶ | next if ref $copy{ $ol }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 1 | my $name = $copy{ $ol }; | |||||||||||||||||||||||||||
| 68 | $copy{ $ol } = sub { | ||||||||||||||||||||||||||||
| 69 | 0 | ▶ | $_[0]->$name(@_[ 1 .. $#_ ]); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 70 | 1 | }; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # We need string eval to set the caller to a variable. -- rjbs, 2009-03-26 | ||||||||||||||||||||||||||||
| 74 | # We must do this before casting magic so that overload.pm can find the | ||||||||||||||||||||||||||||
| 75 | # right entries in the stash to muck with. -- rjbs, 2009-03-26 | ||||||||||||||||||||||||||||
| 76 | 1 | ▶ | die unless eval qq{ | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | package $caller; | ||||||||||||||||||||||||||||
| 78 | use overload %copy; | ||||||||||||||||||||||||||||
| 79 | 1; | ||||||||||||||||||||||||||||
| 80 | }; | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | MRO::Define::register_mro($caller, sub { | ||||||||||||||||||||||||||||
| 84 | 88 | ▶ | return [ undef, $caller ]; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 85 | 6 | }); | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 6 | cast %{"::$caller\::"}, $wiz; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | sub _gen_fetch_magic { | ||||||||||||||||||||||||||||
| 91 | 6 | ▶ | my ($self, $arg) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_gen_fetch_magic: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 6 | my $metamethod = $arg->{metamethod}; | |||||||||||||||||||||||||||
| 94 | 6 | my $passthru = $arg->{passthru}; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 7 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 97 | return sub { | ||||||||||||||||||||||||||||
| 98 | 146 | ▶ | return if $_[2] ~~ $passthru; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 137 | ▶ | return if substr($_[2], 0, 1) eq '('; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 124 | ${ $_[1] } = $_[2]; | |||||||||||||||||||||||||||
| 103 | 124 | $_[2] = $metamethod; | |||||||||||||||||||||||||||
| 104 | 124 | mro::method_changed_in('UNIVERSAL'); | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 124 | return; | |||||||||||||||||||||||||||
| 107 | 6 | }; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 7 | 1; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | __END__ | ||||||||||||||||||||||||||||