| 1 | package ex::monkeypatched; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Sub::Name qw<subname>; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Carp qw<croak>; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 10 | ▶ | sub import { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 10 | my $invocant = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 10 | ▶ | my $norequire = @_ && $_[0] && $_[0] eq '-norequire' && shift; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition77%7/9
@_ && $_[0] && $_[0] eq '-norequire' && shift()
@_ and $_[0] and $_[0] eq "-norequire"
@_ and $_[0]
MC/DC50%2/4
@_ && $_[0] && $_[0] eq '-norequire' && shift()
@_
$_[0]
$_[0] eq "-norequire"
shift()
Truth table60%3/5
A and B and C && D
A= @_B= $_[0]C= $_[0] eq "-norequire"D= shift()
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 10 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 9 | ▶ | my @injections = _parse_injections(@_) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | or croak "Usage: use $invocant \$class => %methods | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | or: use $invocant (class => \$class, methods => \\%methods) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | or: use $invocant (method => \$name, implementations => \\%impl)"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 8 | ▶ | _require(map { $_->[0] } @injections) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | if !$norequire; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 6 | _inject_methods(@injections); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 5 | ▶ | sub _require { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_require: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 5 | for (@_) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 8 | (my $as_file = $_) =~ s{::|'}{/}g; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 8 | require "$as_file.pm"; # dies if no such file is found | |||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 12 | ▶ | sub _parse_injections { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_injections: called CC=9 SCAR=22.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 12 | ▶ | if (@_ == 1 && ref $_[0] eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
@_ == 1 and ref $_[0] eq "HASH"
MC/DC50%1/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1
ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 2 | my $opt = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 2 | ▶ | if (defined $opt->{class} && ref $opt->{methods} eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
defined $opt->{"class"} and ref $opt->{"methods"} eq "HASH"
MC/DC50%1/2
defined $opt->{"class"} and ref $opt->{"methods"} eq "HASH"
defined $opt->{"class"}
ref $opt->{"methods"} eq "HASH"
Truth table66%2/3
A and B
A= defined $opt->{"class"}B= ref $opt->{"methods"} eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 2 | return map { [$opt->{class}, $_, $opt->{methods}{$_}] } | |||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 1 | keys %{ $opt->{methods} }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | ▶ | elsif (defined $opt->{method} && ref $opt->{implementations} eq 'HASH') { | |||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
defined $opt->{"method"} and ref $opt->{"implementations"} eq "HASH"
MC/DC0%0/2
defined $opt->{"method"} and ref $opt->{"implementations"} eq "HASH"
defined $opt->{"method"}
ref $opt->{"implementations"} eq "HASH"
Truth table33%1/3
A and B
A= defined $opt->{"method"}B= ref $opt->{"implementations"} eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 2 | return map { [$_, $opt->{method}, $opt->{implementations}{$_}] } | |||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 1 | keys %{ $opt->{implementations} }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | elsif (@_ % 2) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 9 | my @injections; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 9 | my $target = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 9 | ▶ | push @injections, [$target, splice @_, 0, 2] | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | while @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 9 | return @injections; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 3 | ▶ | sub inject { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
inject: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 3 | my $invocant = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 3 | ▶ | my @injections = _parse_injections(@_) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | or croak "Usage: $invocant->inject(\$class, %methods) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | or: $invocant->inject({ class => \$class, methods => \\%methods }) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | or: $invocant->inject({ method => \$name, implementations => \\%impl })"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 3 | _inject_methods(@injections); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 9 | ▶ | sub _inject_methods { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_inject_methods: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 9 | for (@_) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 15 | my ($target, $name, undef) = @$_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 15 | ▶ | croak qq[Can't monkey-patch: $target already has a method "$name"] | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | if $target->can($name); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 6 | _install_subroutine(@$_) for @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 10 | ▶ | sub _install_subroutine { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_install_subroutine: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 10 | my ($target, $name, $code) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 10 | my $full_name = "$target\::$name"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 10 | my $renamed_code = subname($full_name, $code); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 3 | ▶ | no strict qw<refs>; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 10 | *$full_name = $renamed_code; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||