blib/lib/Arguments.pm

filter: Statementstmt 38.5% 47 / 122 Branchbran 0.0% 0 / 32 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 71.4% 15 / 21 Podpod 0.0% 0 / 3 totaltotal 33.1% 64 / 193 CC 22 SCAR 38.8
CC
22
Cov
30%
process_delayed_checks
144.3
synthesize_call_wrapper
32.5
_quote_strings
12.0
CRAP 48.4
↑ Summary
1 #
2 # Arguments.pm -- Perl subroutine type-checking
3 #
4 # $Id: Arguments.pm,v 1.2 2001/10/07 21:38:35 binkley Exp $
5 #
6  
7 package Arguments;
8  
9 1 ▶use v5.6;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use constant RCSID =>
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 '$Id: Arguments.pm,v 1.2 2001/10/07 21:38:35 binkley Exp $';
15  
16 1 ▶use Carp ( );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Devel::Peek ( );
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # 'our' won't work since we haven't been processed yet when
20 # MODIFY_CODE_ATTRIBUTES is called. Weird. --bko FIXME
21 1 ▶use vars qw(@DELAYED_CHECKS %ARGUMENT_CHECKS);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # Evil sets in. Arrange for ourselves to be in the importer's @ISA so
24 # that MODIFY_CODE_ATTRIBUTES works without the importer needing to
25 # declare us as a base package.
26 1 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
27 1 ▶ no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 1 my $caller = caller;
30 1 push @{"$caller\::ISA"}, __PACKAGE__;
31 }
32  
33 # Add them rather than redefine the hash so that other modules have a
34 # chance to install their own during BEGIN before we are compiled.
35 0 ▶$ARGUMENT_CHECKS{REF} ||= sub { UNIVERSAL::isa ($_[0], $_[1]) };
Subroutine __ANON__: not called CC=1 SCAR=2.0
Condition33%1/3
$Arguments::ARGUMENT_CHECKS{"REF"} ||= sub { UNIVERSAL::isa($_[0], $_[1]); }
l!l&&r!l&&!r
010
MC/DC0%0/2
$Arguments::ARGUMENT_CHECKS{"REF"} ||= sub { UNIVERSAL::isa($_[0], $_[1]); }
$Arguments::ARGUMENT_CHECKS{"REF"} sub { UNIVERSAL::isa($_[0], $_[1]); }
Truth table33%1/3
A ||= B
A= $Arguments::ARGUMENT_CHECKS{"REF"}B= sub { UNIVERSAL::isa($_[0], $_[1]); }
ABresult
1X1
011
000
36 0 ▶$ARGUMENT_CHECKS{RX} ||= sub { defined $_[0] and $_[0] =~ $_[1] };
Subroutine __ANON__: not called CC=2 SCAR=12.4
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and $_[0] =~ /$_[1]/ 0 0 0
Condition33%1/3
$Arguments::ARGUMENT_CHECKS{"RX"} ||= sub { defined $_[0] and $_[0] =~ /$_[1]/; }
l!l&&r!l&&!r
010
MC/DC0%0/2
$Arguments::ARGUMENT_CHECKS{"RX"} ||= sub { defined $_[0] and $_[0] =~ /$_[1]/; }
$Arguments::ARGUMENT_CHECKS{"RX"} sub { defined $_[0] and $_[0] =~ /$_[1]/; }
Truth table33%1/3
A ||= B
A= $Arguments::ARGUMENT_CHECKS{"RX"}B= sub { defined $_[0] and $_[0] =~ /$_[1]/; }
ABresult
1X1
011
000
37  
38 # Convience.
39 1 our $Arguments_Package = __PACKAGE__;
40  
41 # For error messages
42 0 ▶sub _quote_strings {
Subroutine _quote_strings: not called CC=3 SCAR=24.8
43 0 my @s;
44  
45 0 for (@_) {
46 0 ▶ push @s, (defined $_ ? do {
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
47 0 my $s = $_;
48 0 $s =~ s/\\/\\\\/g;
49 0 $s =~ s/'/\\'/g;
50 0 "'$s'";
51 } : 'undef');
52 }
53  
54 0 @s;
55 }
56  
57 # Cribbed from dumpvar.pl.
58 0 ▶sub find_sub_name ($) {
Subroutine find_sub_name: not called CC=2 SCAR=17.9
Pod undocumented
59 0 my $code = shift;
60 0 $code = \&$code; # guarantee a hard reference
61  
62 0 ▶ my $gv = Devel::Peek::CvGV ($code) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $gv = Devel::Peek::CvGV($code) or return 0 0 0
63  
64 0 *$gv{PACKAGE} . '::' . *$gv{NAME};
65 }
66  
67 # We don't want to be bothered by "%s package attribute may clash with
68 # future reserved word: %s" for MODIFY_CODE_ATTRIBUTES. HOW DO YOU
69 # MAKE THIS WORK?? --bko XXX
70 {
71 1 ▶ no warnings qw(reserved);
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 0 ▶ sub MODIFY_CODE_ATTRIBUTES {
Subroutine MODIFY_CODE_ATTRIBUTES: not called CC=1 SCAR=6.9
74 0 my ($package, $coderef, @attributes) = @_;
75  
76 my @arguments = map {
77 0 my $s = $_;
78 0 $s =~ s/^$Arguments_Package\s*\(\s*//;
79 0 $s =~ s/\s*\)$//;
80 0 split /\s*,\s*/, $s;
81 } grep /^$Arguments_Package\s*\(/, @attributes;
82  
83 0 if (0) {
84 # Collect the true source of any problems.
85 my @caller = qw(package filename line subroutine hasargs wantarray
86 evaltext is_require hints bitmask);
87 my %caller;
88 @caller{@caller} = do { package DB; caller (1) };
89  
90 push @DELAYED_CHECKS,
91 [$package, $coderef, [@arguments], {%caller}];
92  
93 } else {
94 0 my $longmess;
95  
96 {
97 0 local $Carp::CarpLevel = 1;
98 0 $longmess = Carp::longmess ('');
99 }
100  
101 0 $longmess =~ s/\n.*//s;
102  
103 # The funky last argument is so that croak looks right
104 0 push @DELAYED_CHECKS,
105 [$package, $coderef, [@arguments], $longmess];
106 }
107  
108 0 grep !/^$Arguments_Package\s*\(/, @attributes;
109 }
110 }
111  
112 1 ▶sub synthesize_call_wrapper ($$$$@) {
Subroutine synthesize_call_wrapper: not called CC=7 SCAR=34.8
Pod undocumented
113 0 my ($package, $sub_name, $prototype, $longmess, @arguments) = @_;
114 0 my $required = grep !/\?$/, @arguments;
115 0 my $optional = @arguments;
116  
117 0 my $coderef;
118 0 ▶ { no strict qw(refs); $coderef = *{$sub_name}{CODE} }
Subroutine BEGIN: called CC=1 SCAR=1.2
119  
120 0 my $s = "sub ($prototype) {
121 Carp::croak \"Not enough arguments for $sub_name\"
122 if \@_ < $required;
123 Carp::croak \"Too many arguments for $sub_name\"
124 if \@_ > $optional;
125 ";
126  
127 0 my $i = 0;
128  
129 0 for my $a (@arguments) {
130 0 my $j = $i + 1;
131  
132 0 $s .= " Carp::croak \"Type of arg $j to $sub_name must be $a (not \"
133 . defined \$_[$i] ? \$_[$i] : 'undef' . ')'
134 ";
135  
136 # How to handle these? --bko FIXME
137 0 my $opt = $a =~ s/\?$//;
138  
139 0 ▶ if (exists $ARGUMENT_CHECKS{$a}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $Arguments::ARGUMENT_CHECKS{$a}) { } 0 0 0
elsif ($a =~ m[^/]) { } 0 0 0
140 0 $s .= " unless \$ARGUMENT_CHECKS{'$a'}->(\$_[$i]);
141 ";
142  
143 } elsif ($a =~ /^\//) {
144 0 eval "use strict; use warnings; qr$a";
145  
146 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
147 # Hide the eval
148 0 my ($s) = $@ =~ /(.*) at .*$/;
149 0 Carp::croak "$s$longmess.\n"; # test RX first
150 }
151  
152 0 $s .= " unless \$ARGUMENT_CHECKS{RX}->(\$_[$i], qr$a);
153 ";
154  
155 } else {
156 0 eval "use strict; use warnings; \${'$a'};";
157  
158 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
159 # Hide the eval
160 0 my ($s) = $@ =~ /(.*) at .*$/;
161 0 Carp::croak "$s$longmess.\n"; # test RX first
162 }
163  
164  
165 0 $s .= " unless \$ARGUMENT_CHECKS{REF}->(\$_[$i], '$a');
166 ";
167 }
168  
169 0 $i = $j;
170 }
171  
172 0 $s .= "
173 goto &\$coderef;
174 };
175 ";
176  
177 # The 'misc' warning is weird -- eval keep seeing "unrecognized
178 # escape \d" while dealing with /^\d+$/ (an unsigned integer), which
179 # doesn't seem right. --bko FIXME
180 0 ▶ { no strict qw(refs); no warnings qw(misc redefine);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
181 0 *{$sub_name} = eval $s }
182  
183 0 ▶ croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
184 }
185  
186 1 ▶sub process_delayed_checks ( ) {
Subroutine process_delayed_checks: called CC=12 SCAR=49.7
Pod undocumented
187 1 for (@DELAYED_CHECKS) {
188 0 my ($package, $coderef, $arguments, $longmess) = @$_;
189 0 my @arguments = @$arguments;
190  
191 0 my $prototype = prototype $coderef;
192  
193 # Normal, variadic sub.
194 0 ▶ return if not defined $prototype and not @arguments;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prototype or @arguments 0 0 0
Condition0%0/3
defined $prototype or @arguments
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $prototype or @arguments
defined $prototype @arguments
Truth table0%0/3
A or B
A= defined $prototypeB= @arguments
ABresult
1X1
011
000
195  
196 0 my $sub_name = find_sub_name $coderef;
197 0 my (@prototypes, $ref, $opt);
198  
199 0 for my $token (split //, $prototype) {
200 0 ▶ if ($ref) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref) { } 0 0 0
elsif ($token eq ';') { } 0 0 0
elsif ($token eq '\\') { } 0 0 0
201 0 undef $ref;
202 0 ▶ push @prototypes, $opt ? "\\$token?" : "\\$token";
Branch: 1 branch
BranchTrueFalseTotal
$opt ? : 0 0 0
203  
204 } elsif ($token eq ';') {
205 0 $opt = 1;
206  
207 } elsif ($token eq "\\") {
208 0 $ref = 1;
209  
210 } else {
211 0 ▶ push @prototypes, $opt ? "$token?" : $token;
Branch: 1 branch
BranchTrueFalseTotal
$opt ? : 0 0 0
212 }
213 }
214  
215 # Check that they match. Use the "\n" trick from Carp::Heavy.
216 0 ▶ Carp::croak "Not enough prototypes for $sub_name$longmess.\n"
Branch: 1 branch
BranchTrueFalseTotal
if @arguments < @prototypes 0 0 0
217 if @arguments < @prototypes;
218 0 ▶ Carp::croak "Too many prototypes for $sub_name$longmess.\n"
Branch: 1 branch
BranchTrueFalseTotal
if @arguments > @prototypes 0 0 0
219 if @arguments > @prototypes;
220  
221 0 synthesize_call_wrapper
222 ($package, $sub_name, $prototype, $longmess, @arguments);
223 }
224 }
225  
226 # Work around that subs don't have prototypes defined yet at the time
227 # that attributes are processed. I'd consider this a bug. --bko FIXME
228 {
229 # We need to be in the main package do delay our processing until
230 # all the other packages have had a chance to declare and/or define
231 # their prototypes. Otherwise, we get called too soon, and
232 # encounter the 'no prototypes' bug. --bko FIXME
233 package main;
234  
235 1 ▶ CHECK {
Subroutine CHECK: called CC=1 SCAR=0.0
236 # This will try to move the problem so that it shows up in the sub
237 # declaration rather that in this processing.
238 1 local $Carp::CarpLevel = 3;
239 1 Arguments::process_delayed_checks ( );
240 }
241 }
242  
243 1 1;
244  
245 __END__
↑ Summary