| 1 | package Sub::Override; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Scalar::Util qw(set_prototype); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | our $VERSION = '0.12'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 7 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 12 | 7 | my $class = shift; | |||||||||||||||||||||||||||
| 13 | 7 | my $self = bless {}, $class; | |||||||||||||||||||||||||||
| 14 | 7 | ▶ | $self->replace(@_) if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 15 | 7 | return $self; | |||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | { | ||||||||||||||||||||||||||||
| 19 | 1 | ▶ | no warnings 'once'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | # because override() was a better name and this is what it should have been | ||||||||||||||||||||||||||||
| 21 | # called. | ||||||||||||||||||||||||||||
| 22 | 1 | *override = *replace{CODE}; | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 11 | ▶ | sub replace { | ||||||||||||||||||||||||||
|
Subroutine
replace: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 26 | 11 | my ( $self, $sub_to_replace, $new_sub ) = @_; | |||||||||||||||||||||||||||
| 27 | 11 | $sub_to_replace = $self->_get_fully_qualified_sub_name($sub_to_replace); | |||||||||||||||||||||||||||
| 28 | 11 | $self->_ensure_code_slot_exists($sub_to_replace)->_validate_sub_ref($new_sub); | |||||||||||||||||||||||||||
| 29 | { | ||||||||||||||||||||||||||||
| 30 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 9 | ▶ | $self->{$sub_to_replace} ||= *$sub_to_replace{CODE}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{$sub_to_replace} ||= *$sub_to_replace{"CODE"}
MC/DC100%1/1
$self->{$sub_to_replace} ||= *$sub_to_replace{"CODE"}
$self->{$sub_to_replace}
Truth table100%2/2
A
A= $self->{$sub_to_replace}
| |||||||||||||||||||||||||||||
| 32 | 1 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 9 | *$sub_to_replace = $new_sub; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | 9 | return $self; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 2 | ▶ | sub inject { | ||||||||||||||||||||||||||
|
Subroutine
inject: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 39 | 2 | my ( $self, $sub_to_inject, $new_sub ) = @_; | |||||||||||||||||||||||||||
| 40 | 2 | $sub_to_inject = $self->_get_fully_qualified_sub_name($sub_to_inject); | |||||||||||||||||||||||||||
| 41 | 2 | $self->_ensure_code_slot_does_not_exist($sub_to_inject)->_validate_sub_ref($new_sub); | |||||||||||||||||||||||||||
| 42 | { | ||||||||||||||||||||||||||||
| 43 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 1 | $self->{$sub_to_inject} = undef; | |||||||||||||||||||||||||||
| 45 | 1 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | 1 | *$sub_to_inject = $new_sub; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | 1 | return $self; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 4 | ▶ | sub inherit { | ||||||||||||||||||||||||||
|
Subroutine
inherit: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | 4 | my ( $self, $sub_to_inherit, $new_sub ) = @_; | |||||||||||||||||||||||||||
| 53 | 4 | $sub_to_inherit = $self->_get_fully_qualified_sub_name($sub_to_inherit); | |||||||||||||||||||||||||||
| 54 | 4 | $self->_ensure_code_slot_exists_in_parent_class($sub_to_inherit)->_validate_sub_ref($new_sub); | |||||||||||||||||||||||||||
| 55 | { | ||||||||||||||||||||||||||||
| 56 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 57 | 1 | $self->{$sub_to_inherit} = undef; | |||||||||||||||||||||||||||
| 58 | 1 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 59 | 1 | *$sub_to_inherit = $new_sub; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | 1 | return $self; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 2 | ▶ | sub wrap { | ||||||||||||||||||||||||||
|
Subroutine
wrap: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | 2 | my ( $self, $sub_to_replace, $new_sub ) = @_; | |||||||||||||||||||||||||||
| 66 | 2 | $sub_to_replace = $self->_get_fully_qualified_sub_name($sub_to_replace); | |||||||||||||||||||||||||||
| 67 | 2 | $self->_ensure_code_slot_exists($sub_to_replace)->_validate_sub_ref($new_sub); | |||||||||||||||||||||||||||
| 68 | { | ||||||||||||||||||||||||||||
| 69 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 70 | 2 | ▶ | $self->{$sub_to_replace} ||= *$sub_to_replace{CODE}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{$sub_to_replace} ||= *$sub_to_replace{"CODE"}
MC/DC0%0/1
$self->{$sub_to_replace} ||= *$sub_to_replace{"CODE"}
$self->{$sub_to_replace}
Truth table50%1/2
A
A= $self->{$sub_to_replace}
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | # passing $sub_to_replace directly to arguments prevents early destruction. | ||||||||||||||||||||||||||||
| 73 | 2 | my $weakened_sub_to_replace = $self->{$sub_to_replace}; | |||||||||||||||||||||||||||
| 74 | 2 | ▶ | my $code = sub { unshift(@_, $weakened_sub_to_replace); goto &$new_sub }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 75 | 2 | my $prototype = prototype($self->{$sub_to_replace}); | |||||||||||||||||||||||||||
| 76 | 2 | ▶ | set_prototype(\&$code, $prototype) if defined $prototype; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 1 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 79 | 2 | *$sub_to_replace = $code; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | 2 | return $self; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 10 | ▶ | sub restore { | ||||||||||||||||||||||||||
|
Subroutine
restore: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 10 | my ( $self, $name_of_sub ) = @_; | |||||||||||||||||||||||||||
| 86 | 10 | $name_of_sub = $self->_get_fully_qualified_sub_name($name_of_sub); | |||||||||||||||||||||||||||
| 87 | 10 | ▶ | if ( !$name_of_sub && 1 == keys %$self ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $name_of_sub and 1 == keys %$self
MC/DC100%2/2
not $name_of_sub and 1 == keys %$self
not $name_of_sub
1 == keys %$self
Truth table100%3/3
not(A) and B
A= not $name_of_subB= 1 == keys %$self
| |||||||||||||||||||||||||||||
| 88 | 1 | ($name_of_sub) = keys %$self; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | croak( | ||||||||||||||||||||||||||||
| 91 | 10 | ▶ | sprintf 'You must provide the name of a sub to restore: (%s)' => join | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | ', ' => sort keys %$self ) | ||||||||||||||||||||||||||||
| 93 | unless $name_of_sub; | ||||||||||||||||||||||||||||
| 94 | croak("Cannot restore a sub that was not replaced ($name_of_sub)") | ||||||||||||||||||||||||||||
| 95 | 9 | ▶ | unless exists $self->{$name_of_sub}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 98 | 1 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 99 | 7 | my $maybe_sub_ref = delete $self->{$name_of_sub}; | |||||||||||||||||||||||||||
| 100 | 7 | ▶ | if ( defined $maybe_sub_ref ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 5 | *$name_of_sub = $maybe_sub_ref; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | else { | ||||||||||||||||||||||||||||
| 104 | 2 | undef *$name_of_sub; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | 7 | return $self; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 7 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 110 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 111 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 112 | # "misc" suppresses warning: 'Undefined value assigned to typeglob' | ||||||||||||||||||||||||||||
| 113 | 1 | ▶ | no warnings 'redefine', 'misc'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 114 | 7 | ▶ | while ( my ( $sub_name, $maybe_sub_ref ) = each %$self ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 4 | ▶ | if ( defined $maybe_sub_ref ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 4 | *$sub_name = $maybe_sub_ref; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | else { | ||||||||||||||||||||||||||||
| 119 | 0 | undef *$sub_name; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 29 | ▶ | sub _get_fully_qualified_sub_name { | ||||||||||||||||||||||||||
|
Subroutine
_get_fully_qualified_sub_name: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 125 | 29 | my ( $self, $subname ) = @_; | |||||||||||||||||||||||||||
| 126 | 29 | ▶ | if ( ( $subname || '' ) =~ /^\w+$/ ) { # || "" for suppressing test warnings | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$subname || ''
MC/DC100%1/1
$subname || ''
$subname
Truth table100%2/2
A
A= $subname
| |||||||||||||||||||||||||||||
| 127 | 14 | my $package = do { | |||||||||||||||||||||||||||
| 128 | 14 | my $call_level = 0; | |||||||||||||||||||||||||||
| 129 | 14 | my $this_package; | |||||||||||||||||||||||||||
| 130 | 14 | ▶ | while ( !$this_package || __PACKAGE__ eq $this_package ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $this_package or __PACKAGE__ eq $this_package
MC/DC100%2/2
not $this_package or __PACKAGE__ eq $this_package
not $this_package
__PACKAGE__ eq $this_package
Truth table100%3/3
not(A) or B
A= not $this_packageB= __PACKAGE__ eq $this_package
| |||||||||||||||||||||||||||||
| 131 | 29 | ($this_package) = caller($call_level); | |||||||||||||||||||||||||||
| 132 | 29 | $call_level++; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | 14 | $this_package; | |||||||||||||||||||||||||||
| 135 | }; | ||||||||||||||||||||||||||||
| 136 | 14 | $subname = "${package}::$subname"; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | 29 | return $subname; | |||||||||||||||||||||||||||
| 139 | }; | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 14 | ▶ | sub _validate_sub_ref { | ||||||||||||||||||||||||||
|
Subroutine
_validate_sub_ref: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 142 | 14 | my ( $self, $sub_ref ) = @_; | |||||||||||||||||||||||||||
| 143 | 14 | ▶ | unless ( 'CODE' eq ref $sub_ref ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 1 | croak("($sub_ref) must be a code reference"); | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | 13 | return $self; | |||||||||||||||||||||||||||
| 147 | }; | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 13 | ▶ | sub _ensure_code_slot_exists { | ||||||||||||||||||||||||||
|
Subroutine
_ensure_code_slot_exists: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 150 | 13 | my ( $self, $code_slot ) = @_; | |||||||||||||||||||||||||||
| 151 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 152 | 13 | ▶ | unless ( defined *{$code_slot}{CODE} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 1 | croak("Cannot replace non-existent sub ($code_slot)"); | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | 12 | return $self; | |||||||||||||||||||||||||||
| 156 | }; | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 6 | ▶ | sub _ensure_code_slot_does_not_exist { | ||||||||||||||||||||||||||
|
Subroutine
_ensure_code_slot_does_not_exist: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 159 | 6 | my ( $self, $code_slot ) = @_; | |||||||||||||||||||||||||||
| 160 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 161 | 6 | ▶ | if ( defined *{$code_slot}{CODE} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 3 | croak("Cannot create a sub that already exists ($code_slot)"); | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | 3 | return $self; | |||||||||||||||||||||||||||
| 165 | }; | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 4 | ▶ | sub _ensure_code_slot_exists_in_parent_class { | ||||||||||||||||||||||||||
|
Subroutine
_ensure_code_slot_exists_in_parent_class: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 168 | 4 | my ( $self, $code_slot ) = @_; | |||||||||||||||||||||||||||
| 169 | 4 | $self->_ensure_code_slot_does_not_exist($code_slot); | |||||||||||||||||||||||||||
| 170 | { | ||||||||||||||||||||||||||||
| 171 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 172 | 2 | my $class = *{$code_slot}{PACKAGE}; | |||||||||||||||||||||||||||
| 173 | 2 | my $method = *{$code_slot}{NAME}; | |||||||||||||||||||||||||||
| 174 | 2 | ▶ | croak("Sub does not exist in parent class ($code_slot)") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | unless $class->can($method); | ||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | 1 | return $self; | |||||||||||||||||||||||||||
| 178 | }; | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 1 | 1; | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | __END__ | ||||||||||||||||||||||||||||