lib/Sub/Signatures.pm

filter: Statementstmt 78.8% 93 / 118 Branchbran 79.6% 43 / 54 Conditioncond 40.9% 9 / 22 MC/DCmcdc 16.6% 2 / 12 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 3 totaltotal 72.3% 162 / 224 CC 48 SCAR 19.0
CC
48
Cov
75%
__ANON__
12.6
__ANON__
10.0
__ANON__
8.8
CRAP 6.7
↑ Summary
1 package Sub::Signatures;
2 11 $REVISION = '$Id: Signatures.pm,v 1.3 2004/12/05 21:19:33 ovid Exp $';
3 11 $VERSION = '0.21';
4  
5 11 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Filter::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 11 my $CALLPACK;
11 my %SIG;
12  
13 11 my %METHODS;
14  
15 13 ▶sub import {
Subroutine import: called CC=3 SCAR=11.8
16 13 my $class = shift;
17 13 my %props = map { $_ => 1 } @_;
18 13 ($CALLPACK) = caller;
19 13 ▶ $METHODS{$CALLPACK} = exists $props{methods} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $props{'methods'} ? : 5 8 13
20 13 ▶ if ( $ENV{SS_DEBUG} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"SS_DEBUG"}) 0 13 13
21 0 require Data::Dumper;
22 0 Data::Dumper->import;
23 0 $Data::Dumper::Indent = 1;
24 }
25 }
26  
27 35 ▶my $signature = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
28 35 my ( $subname, $parameters ) = @_;
29 35 ▶ if ( 'fallback' eq $parameters ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('fallback' eq $parameters) { } 3 32 35
30 3 return ( "_${subname}_fallback", '', 0 );
31 }
32 else {
33 my @args =
34 32 ▶ map { /\s*(\S*)\s*(\$\w+)/; [ $1 || 'SCALAR', $2 ] }
Condition50%1/2
$1 || 'SCALAR'
l!l
057
MC/DC0%0/1
$1 || 'SCALAR'
$1
Truth table50%1/2
A
A= $1
Aresult
11
00
35 split /(?:,|=>)/ => $parameters;
36 32 my $count = @args;
37 32 $parameters = join ', ' => map { $_->[1] } @args;
38 32 return ( "__${subname}_$count", $parameters, scalar @args );
39 }
40 11 };
41  
42 13 ▶my $make_subs = sub {
Subroutine __ANON__: called CC=10 SCAR=23.1
43 13 ▶ while ( my ( $pack, $subs ) = each %SIG ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %SIG) 17 13 30
44 17 ▶ while ( my ( $sub, $counts ) = each %$subs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$subs) 37 17 54
45 my @build =
46 30 sort { $a->[1] cmp $b->[1] }
47 37 map { [ $_ => $counts->{$_} ] } keys %$counts;
48 37 foreach my $item (@build) {
49 64 my ( $count, $target ) = @$item;
50 64 ▶ next if 0 <= index +( $subs->{$sub}{body} || '' ), $target;
Branch: 1 branch
BranchTrueFalseTotal
if index($subs->{$sub}{"body"} || "", $target) != -1 29 35 64
Condition100%2/2
$subs->{$sub}{'body'} || ''
l!l
4024
MC/DC100%1/1
$subs->{$sub}{'body'} || ''
$subs->{$sub}{'body'}
Truth table100%2/2
A
A= $subs->{$sub}{'body'}
Aresult
11
00
51 35 ▶ next if 'body' eq $count;
Branch: 1 branch
BranchTrueFalseTotal
if "body" eq $count 0 35 35
52 35 ▶ $subs->{$sub}{body} ||= '';
Condition100%2/2
$subs->{$sub}{"body"} ||= ""
l!l
1124
MC/DC100%1/1
$subs->{$sub}{"body"} ||= ""
$subs->{$sub}{"body"}
Truth table100%2/2
A
A= $subs->{$sub}{"body"}
Aresult
11
00
53 35 ▶ $subs->{$sub}{body} .= $target =~ /_fallback$/
Branch: 1 branch
BranchTrueFalseTotal
$target =~ /_fallback$/ ? : 3 32 35
54 ? " goto \&$target;\n"
55 : " goto \&$target if $count == \@_;\n";
56 }
57 }
58 }
59 13 ▶ print Dumper(%SIG) if $ENV{SS_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"SS_DEBUG"} 0 13 13
60 11 };
61  
62 13 ▶my $install_subs = sub {
Subroutine __ANON__: called CC=8 SCAR=25.3
63 13 ▶ while ( my ( $pack, $subs ) = each %SIG ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %SIG) 17 13 30
64 17 foreach my $sub ( keys %$subs ) {
65 37 my $body = $subs->{$sub}{body};
66 37 ▶ my $type = $METHODS{$pack} ? 'method' : 'sub';
Branch: 1 branch
BranchTrueFalseTotal
$METHODS{$pack} ? : 24 13 37
67 37 ▶ unless ( $body =~ /_fallback;/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($body =~ /_fallback;/) 31 6 37
68 31 $body .= <<" END_BODY";
69 # if we got to here, there was no $type to dispatch to
70 require Carp;
71 shift if 'method' eq '$type';
72 my \$types = join ', ' => map { ref \$_ || 'SCALAR' } \@_;
73 Carp::croak "Could not find a $type matching your signature: $sub(\$types)";
74 END_BODY
75 }
76 11 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
77 37 my $installed_sub = "package $pack;\nsub $sub {\n$body}";
78 0 ▶ eval $installed_sub;
Subroutine foo: called CC=4 SCAR=18.5
Subroutine _private: called CC=4 SCAR=18.5
Subroutine bar: called CC=2 SCAR=9.5
Subroutine baz: called CC=3 SCAR=14.6
Subroutine first_four: called CC=3 SCAR=14.6
Pod undocumented
Pod undocumented
Pod undocumented
Branch: 7 branches
BranchTrueFalseTotal
if 1 == @_ 11 7 18
if 2 == @_ 5 1 6
if 1 == @_ 2 7 9
if 2 == @_ 7 0 7
if 2 == @_ 4 1 5
if 2 == @_ 3 0 3
if 1 == @_ 1 0 1
Condition10%1/10
ref $_ || 'SCALAR'
l!l
02
ref $_ || 'SCALAR'
l!l
00
ref $_ || 'SCALAR'
l!l
00
ref $_ || 'SCALAR'
l!l
00
ref $_ || 'SCALAR'
l!l
00
MC/DC0%0/1
ref $_ || 'SCALAR'
ref $_
MC/DC0%0/1
ref $_ || 'SCALAR'
ref $_
MC/DC0%0/1
ref $_ || 'SCALAR'
ref $_
MC/DC0%0/1
ref $_ || 'SCALAR'
ref $_
MC/DC0%0/1
ref $_ || 'SCALAR'
ref $_
Truth table50%1/2
A
A= ref $_
Aresult
11
00
Truth table0%0/2
A
A= ref $_
Aresult
11
00
Truth table0%0/2
A
A= ref $_
Aresult
11
00
Truth table0%0/2
A
A= ref $_
Aresult
11
00
Truth table0%0/2
A
A= ref $_
Aresult
11
00
79 warn
80 "Installing &${pack}::$sub\n----------\n$installed_sub\n----------\n"
81 37 ▶ if !$@ && $ENV{SS_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if not $@ and $ENV{"SS_DEBUG"} 0 37 37
Condition33%1/3
not $@ and $ENV{"SS_DEBUG"}
!ll&&!rl&&r
0370
MC/DC0%0/2
not $@ and $ENV{"SS_DEBUG"}
not $@ $ENV{"SS_DEBUG"}
Truth table33%1/3
not(A) and B
A= not $@B= $ENV{"SS_DEBUG"}
ABresult
0X0
100
111
82 37 ▶ die
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 37 37
83 "Failed to install &${pack}::$sub\n----------\n$installed_sub\n----------\nReason: $@"
84 if $@;
85 }
86 }
87 11 };
88  
89 # each regex can capture itself!
90 11 my $sub_name_re = qr/([_[:alpha:]][[:word:]]*)/;
91 11 my $parameters_re = qr/\(([^)]+)\)/;
92 13 ▶FILTER {
Subroutine __ANON__: called CC=8 SCAR=21.7
93 13 ▶ warn "Calling package: $CALLPACK ****" if $ENV{SS_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"SS_DEBUG"} 0 13 13
94 13 ▶ while (/(sub\s+$sub_name_re?\s*$parameters_re[^{]*\{)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/(sub\s+$sub_name_re?\s*${parameters_re}[^{]*\{)/) 39 13 52
95 39 my ( $sub_with_sig, $oldname, $parameters ) = ( $1, $2, $3 );
96  
97 # the following line doesn't work. For some reason, using prototypes
98 # with this module causes an infinite while loop here.
99 # I'm probably overlooking something really obvious.
100 # next if $parameters =~ /^\s*[\\\$@%*;\[\]]*\s*$/; # ignore prototypes
101  
102 39 my ( $newname, $newparams, $count );
103 39 ▶ if ($oldname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($oldname) { } 35 4 39
104  
105 # named sub
106 35 ( $newname, $newparams, $count ) =
107 $signature->( $oldname, $parameters );
108 35 ▶ if ( exists $SIG{$CALLPACK}{$oldname}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $SIG{$CALLPACK}{$oldname} and exists $SIG{$CALLPACK}{$oldname}{$count}) 0 35 35
Condition66%2/3
exists $SIG{$CALLPACK}{$oldname} and exists $SIG{$CALLPACK}{$oldname}{$count}
!ll&&!rl&&r
24110
MC/DC0%0/2
exists $SIG{$CALLPACK}{$oldname} and exists $SIG{$CALLPACK}{$oldname}{$count}
exists $SIG{$CALLPACK}{$oldname} exists $SIG{$CALLPACK}{$oldname}{$count}
Truth table66%2/3
A and B
A= exists $SIG{$CALLPACK}{$oldname}B= exists $SIG{$CALLPACK}{$oldname}{$count}
ABresult
0X0
100
111
109 && exists $SIG{$CALLPACK}{$oldname}{$count} )
110 {
111 0 my $args = $newname;
112 0 $args =~ s/^_\w+_//;
113 0 $args =~ s/_/, /g;
114  
115 # how do I get the line number?
116 0 die "$oldname($args) redefined in package '$CALLPACK'";
117 }
118 35 $SIG{$CALLPACK}{$oldname}{$count} = $newname;
119 }
120 else {
121  
122 # anonymous sub
123 4 $newname = '';
124 4 $newparams = $parameters;
125 }
126 39 ▶ if ($newparams) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newparams) { } 36 3 39
127 36 s/\Q$sub_with_sig\E/sub $newname \{ my ($newparams) = \@_;/;
128 }
129 else {
130 3 s/\Q$sub_with_sig\E/sub $newname \{/;
131 }
132 }
133 13 $make_subs->();
134 13 $install_subs->();
135 13 ▶ print $_ if $ENV{SS_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"SS_DEBUG"} 0 13 13
136 11 };
137  
138 11 1;
139  
140 __END__
↑ Summary