blib/lib/Sub/Remove.pm

filter: Statementstmt 100% 47 / 47 Branchbran 100% 12 / 12 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 87.5% 7 / 8 Podpod 100% 1 / 1 totaltotal 98.6% 72 / 73 CC 11 SCAR 19.6
CC
11
Cov
100%
sub_remove
11.0
BEGIN
1.0
BEGIN
1.0
CRAP 7.1
↑ Summary
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
BranchTrueFalseTotal
unless (defined $sub_name) 1 4 5
21 1 croak "sub_remove() requires a subroutine name as parameter";
22 }
23  
24 4 ▶ if (! defined $class) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $class) 2 2 4
25 2 $class = 'main';
26 }
27  
28 4 ▶ if (! $class->can($sub_name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->can($sub_name)) 2 2 4
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
BranchTrueFalseTotal
unless defined $sub and defined &$sub 274 29 303
Condition100%3/3
defined $sub and defined &$sub
!ll&&!rl&&r
273129
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless defined $elem 116 29 145
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
BranchTrueFalseTotal
if ($symbol ne $sub_name) 27 2 29
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__
↑ Summary