| 1 | package Sub::Remove; | ||||||||||||||||||||||||||||
| 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 Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 9 | 3 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 10 | sub_remove | ||||||||||||||||||||||||||||
| 11 | ); | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 3 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 0 | ▶ | sub __placeholder {} | ||||||||||||||||||||||||||
|
Subroutine
__placeholder: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 5 | ▶ | sub sub_remove { | ||||||||||||||||||||||||||
|
Subroutine
sub_remove: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | 5 | my ($sub_name, $class) = @_; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 5 | ▶ | if (! defined $sub_name) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 1 | croak "sub_remove() requires a subroutine name as parameter"; | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 4 | ▶ | if (! defined $class) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 2 | $class = 'main'; | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 4 | ▶ | if (! $class->can($sub_name)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 2 | croak "Subroutine named '${class}::${sub_name}' doesn't exist"; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 2 | my $src; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # get the calling package symbol table name | ||||||||||||||||||||||||||||
| 35 | { | ||||||||||||||||||||||||||||
| 36 | 3 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 2 | $src = \%{ $class . '::' }; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # loop through all symbols in calling package, looking for subs | ||||||||||||||||||||||||||||
| 41 | 2 | for my $symbol ( keys %$src ) { | |||||||||||||||||||||||||||
| 42 | # get all code references, make sure they're valid | ||||||||||||||||||||||||||||
| 43 | 303 | my $sub = *{ $src->{$symbol} }{CODE}; | |||||||||||||||||||||||||||
| 44 | 303 | ▶ | next unless defined $sub and defined &$sub; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $sub and defined &$sub
MC/DC100%2/2
defined $sub and defined &$sub
defined $sub
defined &$sub
Truth table100%3/3
A and B
A= defined $subB= defined &$sub
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # save all other slots of the typeglob | ||||||||||||||||||||||||||||
| 47 | 29 | my @slots; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 29 | for my $slot (qw( SCALAR ARRAY HASH IO FORMAT )) { | |||||||||||||||||||||||||||
| 50 | 145 | my $elem = *{ $src->{$symbol} }{$slot}; | |||||||||||||||||||||||||||
| 51 | 145 | ▶ | next unless defined $elem; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 29 | push @slots, $elem; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # clear out the source glob | ||||||||||||||||||||||||||||
| 56 | 29 | undef $src->{$symbol}; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # replace the sub in the source | ||||||||||||||||||||||||||||
| 59 | 29 | ▶ | if ($symbol ne $sub_name) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 4 | ▶ | $src->{$symbol} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 61 | 4 | my @args = @_; | |||||||||||||||||||||||||||
| 62 | 4 | return $sub->(@_); | |||||||||||||||||||||||||||
| 63 | 27 | }; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | # replace the other slot elements | ||||||||||||||||||||||||||||
| 67 | 29 | for my $elem (@slots) { | |||||||||||||||||||||||||||
| 68 | 29 | $src->{$symbol} = $elem; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 3 | 1; | |||||||||||||||||||||||||||
| 74 | __END__ | ||||||||||||||||||||||||||||