Contract/Compiler.pm

filter: Statementstmt 82.1% 272 / 331 Branchbran 75.8% 129 / 170 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 22 / 22 Podpod 100% 3 / 3 totaltotal 80.6% 432 / 536 CC 114 SCAR 24.2
CC
114
Cov
80%
_generate_code
23.0
enable
22.4
contract_bim
12.2
CRAP 11.2
« Contract/Memoizer.pm ↑ Summary Contract.pm »
1 #
2 # Sub::Contract::Compiler - Compile, enable and disable a contract
3 #
4 # $Id: Compiler.pm,v 1.22 2009/06/16 12:23:58 erwan_lemonnier Exp $
5 #
6  
7 package Sub::Contract::Compiler;
8  
9 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 29 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 28 ▶use Carp qw(croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 26 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 26 ▶use Sub::Contract::Debug qw(debug);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 25 ▶use Sub::Name;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 23 our $VERSION = '0.12';
17  
18 #---------------------------------------------------------------
19 #
20 # enable - recompile contract and reenable it
21 #
22  
23 56 ▶sub enable {
Subroutine enable: called CC=17 SCAR=31.1
Pod documented
24 56 my $self = shift;
25  
26 56 debug(1,"Sub::Contract: enabling contract for [".$self->contractor."]");
27  
28 56 ▶ $self->disable if ($self->{is_enabled});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"is_enabled"} 10 46 56
29  
30 # list all variables with same names in enable() as in _generate_code()
31 56 my $contractor = $self->contractor;
32 56 my $validator_in = $self->{in};
33 56 my $validator_out = $self->{out};
34 56 my $check_in = $self->{pre};
35 56 my $check_out = $self->{post};
36 56 my $invariant = $self->{invariant};
37 56 my $cache = $self->{cache};
38  
39 56 my @list_checks_in;
40 my %hash_checks_in;
41 56 ▶ if (defined $validator_in) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $validator_in) 30 26 56
42 30 @list_checks_in = @{$validator_in->list_checks};
43 30 %hash_checks_in = %{$validator_in->hash_checks};
44 }
45  
46 56 my @list_checks_out;
47 my %hash_checks_out;
48 56 ▶ if (defined $validator_out) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $validator_out) 21 35 56
49 21 @list_checks_out = @{$validator_out->list_checks};
50 21 %hash_checks_out = %{$validator_out->hash_checks};
51 }
52  
53 # compile code to validate pre and post constraints
54 56 my $str_pre = _generate_code('before',
55 $contractor,
56 $validator_in,
57 $check_in,
58 $invariant,
59 # a mapping to local variable names
60 {
61 contractor => "contractor",
62 validator => "validator_in",
63 check => "check_in",
64 invariant => "invariant",
65 list_check => "list_checks_in",
66 hash_check => "hash_checks_in",
67 },
68 );
69  
70 56 my $str_post = _generate_code('after',
71 $contractor,
72 $validator_out,
73 $check_out,
74 $invariant,
75 # a mapping to local variable names
76 {
77 contractor => "contractor",
78 validator => "validator_out",
79 check => "check_out",
80 invariant => "invariant",
81 list_check => "list_checks_out",
82 hash_check => "hash_checks_out",
83 },
84 );
85  
86 56 my $str_call_pre = "";
87 56 my $str_call_post = "";
88  
89 56 ▶ if ($str_pre) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str_pre) 41 15 56
90 41 $str_call_pre = q{
91 &$cref_pre();
92 };
93 }
94  
95 56 ▶ if ($str_post) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str_post) 32 24 56
96 32 $str_call_post = q{
97 &$cref_post();
98 };
99 }
100  
101 # find contractor's code ref
102 56 my $cref = $self->contractor_cref;
103  
104 # add caching
105 56 my $str_cache_enter = "";
106 56 my $str_cache_return_array = "";
107 56 my $str_cache_return_scalar = "";
108  
109 56 ▶ if ($cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache) 8 48 56
110 8 ▶ $str_cache_enter = sprintf q{
Branch: 2 branches
BranchTrueFalseTotal
Sub::Contract::Memoizer::_is_profiler_on() ? : 7 1 8
Sub::Contract::Memoizer::_is_profiler_on() ? : 7 1 8
111 if (!defined $Sub::Contract::wantarray) {
112 _croak "calling memoized subroutine %s in void context";
113 }
114  
115 if (grep({ ref $_; } @_)) {
116 _croak "cannot memoize result of %s when input arguments contain references";
117 }
118  
119 my $key = join(":", map( { (defined $_) ? $_ : "undef"; } ( ($Sub::Contract::wantarray) ? "array":"scalar"),@_));
120 if ($cache->has($key)) {
121 %s
122 if ($Sub::Contract::wantarray) {
123 return @{$cache->get($key)};
124 } else {
125 return $cache->get($key);
126 }
127 }
128 %s
129 },
130 $contractor,
131 $contractor,
132 (Sub::Contract::Memoizer::_is_profiler_on()) ? "Sub::Contract::Memoizer::_incr_hit(\"$contractor\");" : "",
133 (Sub::Contract::Memoizer::_is_profiler_on()) ? "Sub::Contract::Memoizer::_incr_miss(\"$contractor\");" : "";
134  
135 8 ▶ $str_cache_return_array = sprintf q{
Branch: 1 branch
BranchTrueFalseTotal
Sub::Contract::Memoizer::_is_profiler_on() ? : 7 1 8
136 $cache->set($key,\@Sub::Contract::results);
137 %s
138 },
139 (Sub::Contract::Memoizer::_is_profiler_on()) ? "Sub::Contract::Memoizer::_incr_max_reached(\"$contractor\");" : "";
140  
141 8 ▶ $str_cache_return_scalar = sprintf q{
Branch: 1 branch
BranchTrueFalseTotal
Sub::Contract::Memoizer::_is_profiler_on() ? : 7 1 8
142 $cache->set($key,$s);
143 %s
144 },
145 (Sub::Contract::Memoizer::_is_profiler_on()) ? "Sub::Contract::Memoizer::_incr_max_reached(\"$contractor\");" : "";
146 }
147  
148 # the context in which the contracted sub is called depends on
149 # whether we have conditions on return values
150 56 my $str_call;
151  
152 56 ▶ if (!defined $validator_out) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $validator_out) { } 35 21 56
153 # there are no constraints on return arguments so we can't assume
154 # anything on the context the sub expects to be called in
155 # we therefore propagate the same context as the call to the contract
156  
157 35 $str_call = sprintf q{
158  
159 local $Sub::Contract::wantarray = wantarray;
160  
161 %s
162  
163 # TODO: this code is not re-entrant. use local variables for args/wantarray/results. is local enough?
164  
165 local @Sub::Contract::args = @_;
166 local @Sub::Contract::results = ();
167  
168 if (!defined $Sub::Contract::wantarray) {
169 # void context
170 %s
171 &$cref(@Sub::Contract::args);
172 @Sub::Contract::results = ();
173 %s
174 return ();
175  
176 } elsif ($Sub::Contract::wantarray) {
177 # array context
178 %s
179 @Sub::Contract::results = &$cref(@Sub::Contract::args);
180 %s
181 %s
182 return @Sub::Contract::results;
183  
184 } else {
185 # scalar context
186 %s
187 my $s = &$cref(@Sub::Contract::args);
188 @Sub::Contract::results = ($s);
189 %s
190 %s
191 return $s;
192 }
193 },
194 $str_cache_enter,
195 $str_call_pre,
196 $str_call_post,
197 $str_call_pre,
198 $str_call_post,
199 $str_cache_return_array,
200 $str_call_pre,
201 $str_call_post,
202 $str_cache_return_scalar;
203  
204 } else {
205 # we have conditions set on the return values
206 # we have 3 cases:
207 21 my @checks = (@list_checks_out,%hash_checks_out);
208  
209 21 ▶ if (scalar @checks == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @checks == 0) { } 10 11 21
elsif (scalar @checks == 1) { } 5 6 11
210 # the sub returns nothing. therefore it should
211 # only be called in void context. anything else
212 # is an error.
213  
214 # we shouldn't try caching this sub
215 10 ▶ if ($cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache) 0 10 10
216 0 croak "trying to cache a sub that returns nothing (according to ->out())";
217 }
218  
219 10 $str_call = sprintf q{
220  
221 local $Sub::Contract::wantarray = wantarray;
222  
223 if (defined $Sub::Contract::wantarray) {
224 _croak "calling %s in scalar or array context when its contract says it has no return values";
225 }
226  
227 local @Sub::Contract::args = @_;
228 local @Sub::Contract::results = ();
229  
230 # void context, but we call the sub in array context to check if we get something back
231 # (if we do, it's an error)
232 %s
233 @Sub::Contract::results = &$cref(@Sub::Contract::args);
234 %s
235 return;
236 },
237 $contractor,
238 $str_call_pre,
239 $str_call_post;
240  
241 } elsif (scalar @checks == 1) {
242 # the sub returns only 1 element.
243 # we don't know though whether it returns a scalar
244 # (most likely) or an array with just 1 element.
245 # returning a 1-element array instead of a scalar
246 # is a sign of bad programming so we just forbid
247 # this case by raising an error if called in array
248 # context.
249 # otherwise, we call the sub in scalar context,
250 # check the result and return it.
251  
252 5 $str_call = sprintf q{
253  
254 local $Sub::Contract::wantarray = wantarray;
255  
256 %s
257  
258 # TODO: this code is not re-entrant. use local variables for args/wantarray/results. is local enough?
259  
260 if ($Sub::Contract::wantarray) {
261 _croak "calling %s in array context when its contract says it returns a scalar";
262 }
263  
264 local @Sub::Contract::args = @_;
265 local @Sub::Contract::results = ();
266  
267 # call in scalar context, even if called from void context
268 %s
269 my $s = &$cref(@Sub::Contract::args);
270 @Sub::Contract::results = ($s);
271 %s
272 %s
273 return $s;
274  
275 },
276 $str_cache_enter,
277 $contractor,
278 $str_call_pre,
279 $str_call_post,
280 $str_cache_return_scalar;
281  
282 } else {
283 # the sub returns an array. we call it in array context,
284 # check the conditions and return an array as well
285  
286 6 $str_call = sprintf q{
287  
288 local $Sub::Contract::wantarray = wantarray;
289  
290 %s
291  
292 # TODO: this code is not re-entrant. use local variables for args/wantarray/results. is local enough?
293  
294 local @Sub::Contract::args = @_;
295 local @Sub::Contract::results = ();
296  
297 # call in array context, even if called from void or scalar context
298 %s
299 @Sub::Contract::results = &$cref(@Sub::Contract::args);
300 %s
301 %s
302 return @Sub::Contract::results;
303  
304 },
305 $str_cache_enter,
306 $str_call_pre,
307 $str_call_post,
308 $str_cache_return_array;
309 }
310 }
311  
312 56 my $str_contract = sprintf q{
313 use Carp;
314  
315 my $cref_pre = sub {
316 %s
317 };
318  
319 my $cref_post = sub {
320 %s
321 };
322  
323 $contract = sub {
324 %s
325 }
326 },
327 $str_pre,
328 $str_post,
329 $str_call;
330  
331 # compile code
332 56 $str_contract =~ s/^\s+//gm;
333  
334 56 debug(2,join("\n",
335 "Sub::Contract: wrapping this code around [".$self->contractor."]:",
336 "-------------------------------------------------------",
337 $str_contract,
338 "-------------------------------------------------------"));
339  
340 56 my $contract;
341 0 ▶ eval $str_contract;
Subroutine BEGIN: called CC=1 SCAR=0.4
Subroutine contract_foo: called CC=3 SCAR=12.1
Subroutine BEGIN: called CC=1 SCAR=0.4
Subroutine BEGIN: called CC=1 SCAR=0.4
Subroutine BEGIN: called CC=1 SCAR=0.4
Subroutine BEGIN: called CC=1 SCAR=0.4
Branch: 48 branches
BranchTrueFalseTotal
if (not defined $Sub::Contract::wantarray) { } 78 89 167
elsif ($Sub::Contract::wantarray) { } 5 61 66
if (defined $Sub::Contract::wantarray) 57 0 57
if (not defined $Sub::Contract::wantarray) { } 22 5 27
elsif ($Sub::Contract::wantarray) { } 17 10 27
if ($Sub::Contract::wantarray) 15 2 17
if (defined $Sub::Contract::wantarray) 0 10 10
if (defined $Sub::Contract::wantarray) 7 3 10
if (defined $Sub::Contract::wantarray) 1 27 28
if ($Sub::Contract::wantarray) 1 26 27
if ($Sub::Contract::wantarray) 68 0 68
if ($Sub::Contract::wantarray) 0 26 26
unless (defined $Sub::Contract::wantarray) 20 6 26
if (grep {ref $_;} @_) 0 20 20
defined $_ ? : 0 6 6
$Sub::Contract::wantarray ? : 0 6 6
if ($cache->has($key)) 0 13 13
if ($Sub::Contract::wantarray) { } 0 13 13
if (not defined $Sub::Contract::wantarray) { } 26 0 26
elsif ($Sub::Contract::wantarray) { } 0 13 13
unless (defined $Sub::Contract::wantarray) 1 12 13
if (grep {ref $_;} @_) 0 1 1
defined $_ ? : 0 12 12
$Sub::Contract::wantarray ? : 0 12 12
if ($cache->has($key)) 0 1 1
if ($Sub::Contract::wantarray) { } 0 1 1
if (not defined $Sub::Contract::wantarray) { } 4 0 4
elsif ($Sub::Contract::wantarray) { } 1 0 1
unless (defined $Sub::Contract::wantarray) 0 1 1
if (grep {ref $_;} @_) 0 0 0
defined $_ ? : 0 1 1
$Sub::Contract::wantarray ? : 1 0 1
if ($cache->has($key)) 0 2 2
if ($Sub::Contract::wantarray) { } 0 2 2
if (not defined $Sub::Contract::wantarray) { } 8 0 8
elsif ($Sub::Contract::wantarray) { } 2 0 2
unless (defined $Sub::Contract::wantarray) 1 1 2
if (grep {ref $_;} @_) 1 0 1
defined $_ ? : 0 1 1
$Sub::Contract::wantarray ? : 1 0 1
if ($cache->has($key)) 0 2 2
if ($Sub::Contract::wantarray) { } 0 2 2
if (not defined $Sub::Contract::wantarray) { } 6 0 6
elsif ($Sub::Contract::wantarray) { } 0 2 2
unless (defined $Sub::Contract::wantarray) 1 1 2
if (grep {ref $_;} @_) 0 1 1
defined $_ ? : 0 1 1
$Sub::Contract::wantarray ? : 0 1 1
342  
343 56 ▶ if (defined $@ and $@ ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $@ and $@ ne "") 0 56 56
Condition33%1/3
defined $@ and $@ ne ""
!ll&&!rl&&r
0560
MC/DC0%0/2
defined $@ and $@ ne ""
defined $@ $@ ne ""
Truth table33%1/3
A and B
A= defined $@B= $@ ne ""
ABresult
0X0
100
111
344 0 confess "BUG: failed to compile contract ($@)";
345 }
346  
347 # replace contractor with contract sub
348 56 $^W = 0;
349 23 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
350 23 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
351 56 *{ $self->contractor } = $contract;
352  
353 56 my $name = $self->contractor;
354 56 $name =~ s/::([^:]+)$/::contract_$1/;
355 56 subname $name, $contract;
356  
357 56 $self->{is_enabled} = 1;
358  
359 56 return $self;
360 }
361  
362 15 ▶sub disable {
Subroutine disable: called CC=2 SCAR=6.9
Pod documented
363 15 my $self = shift;
364 15 ▶ if ($self->{is_enabled}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"is_enabled"}) 15 0 15
365 15 debug(1,"Sub::Contract: disabling contract on [".$self->contractor."]");
366  
367 # restore original sub
368 15 $^W = 0;
369 23 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
370 23 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
371 15 *{ $self->contractor } = $self->{contractor_cref};
372  
373 # TODO: remove memoization
374 15 $self->{is_enabled} = 0;
375 }
376 15 return $self;
377  
378 }
379  
380 12 ▶sub is_enabled {
Subroutine is_enabled: called CC=1 SCAR=0.0
Pod documented
381 12 return $_[0]->{is_enabled};
382 }
383  
384 #---------------------------------------------------------------
385 #
386 # _compile - generate the code to validate the contract before
387 # or after a call to the contractor function
388 #
389  
390 # TODO: insert _croak inline in compiled code
391 # croak from contract code, with proper stack level
392 181 ▶sub _croak {
Subroutine _croak: called CC=1 SCAR=0.0
393 181 my $msg = shift;
394 181 local $Carp::CarpLevel = 2;
395 181 confess "contract failed: $msg";
396 }
397  
398 # TODO: insert _run inline in compiled code
399 # run a condition, with proper stack level if croak
400 370 ▶sub _run {
Subroutine _run: called CC=1 SCAR=0.0
401 370 my ($func,@args) = @_;
402 370 local $Carp::CarpLevel = 4;
403 370 my $res = $func->(@args);
404 359 local $Carp::CarpLevel = 0; # is this needed? isn't local doing its job?
405 359 return $res;
406 }
407  
408 # The strategy we use for building the contract validation sub is to
409 # to (quite horribly) build a string containing the code of the validation sub,
410 # then compiling this code with eval. We could instead use a closure,
411 # but that would mean that many things we can test at compile time would
412 # end up being tested each time the closure is called which would be a
413 # waste of cpu.
414  
415 112 ▶sub _generate_code {
Subroutine _generate_code: called CC=23 SCAR=31.4
416 112 my ($state,$contractor,$validator,$check_condition,$check_invariant,$varnames) = @_;
417 112 my (@list_checks,%hash_checks);
418  
419 112 ▶ croak "BUG: wrong state" if ($state !~ /^before|after$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $state =~ /^before|after$/ 0 112 112
420  
421 # the code validating the pre or post-call part of the contract, as a string
422 112 my $str_code = "";
423  
424 # code validating the contract invariant
425 112 ▶ if (defined $check_invariant) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $check_invariant) 18 94 112
426 $str_code .= sprintf q{
427 if (!_run($%s,@Sub::Contract::args)) {
428 _croak "invariant fails %s calling $%s";
429 }
430 18 }, $varnames->{invariant}, $state, $varnames->{contractor};
431 }
432  
433 # code validating the contract pre/post condition
434 112 ▶ if (defined $check_condition) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $check_condition) 9 103 112
435 9 ▶ if ($state eq 'before') {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq 'before') { } 4 5 9
436 $str_code .= sprintf q{
437 if (!_run($%s,@Sub::Contract::args)) {
438 _croak "pre-condition fails before calling $%s";
439 }
440 4 }, $varnames->{check}, $varnames->{contractor};
441 } else {
442 # if the contractor is called without context, the result is set to ()
443 # so we can't validate the returned arguments. maybe we should issue a warning?
444 $str_code .= sprintf q{
445 if (!_run($%s,@Sub::Contract::results)) {
446 _croak "post-condition fails after calling $%s";
447 }
448 5 }, $varnames->{check}, $varnames->{contractor};
449 }
450 }
451  
452 # compile the arguments validation code
453 112 ▶ if (defined $validator) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $validator) 51 61 112
454  
455 51 @list_checks = @{$validator->list_checks};
456 51 %hash_checks = %{$validator->hash_checks};
457  
458 # get args/@_ from right source
459 51 ▶ if ($state eq 'before') {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq 'before') { } 30 21 51
460 30 $str_code .= q{ my @args = @Sub::Contract::args; };
461 } else {
462 21 $str_code .= q{ my @args = @Sub::Contract::results; };
463 }
464  
465 # if arguments are list style only, check their count
466 51 ▶ if (!$validator->has_hash_args) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($validator->has_hash_args) 39 12 51
467 39 my $count = scalar @list_checks;
468 39 ▶ if ($state eq 'before') {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq 'before') { } 22 17 39
469 $str_code .= sprintf q{
470 _croak "$%s expected %s input arguments but got ".(scalar @args) if (scalar @args != %s);
471 },
472 $varnames->{contractor},
473 22 ▶ ($count == 0) ? "no" : "exactly $count",
Branch: 1 branch
BranchTrueFalseTotal
$count == 0 ? : 10 12 22
474 $count;
475 } else {
476 $str_code .= sprintf q{
477 _croak "$%s should return %s values but returned ".(scalar @args) if (scalar @args != %s);
478 },
479 $varnames->{contractor},
480 17 ▶ ($count == 0) ? "no" : "exactly $count",
Branch: 1 branch
BranchTrueFalseTotal
$count == 0 ? : 10 7 17
481 $count;
482 }
483 }
484  
485 # do we have arguments to validate?
486 51 ▶ if ($validator->has_list_args || $validator->has_hash_args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($validator->has_list_args or $validator->has_hash_args) 31 20 51
Condition100%3/3
$validator->has_list_args or $validator->has_hash_args
l!l&&r!l&&!r
26520
MC/DC100%2/2
$validator->has_list_args or $validator->has_hash_args
$validator->has_list_args $validator->has_hash_args
Truth table100%3/3
A or B
A= $validator->has_list_argsB= $validator->has_hash_args
ABresult
1X1
011
000
487  
488 # add code validating heading arguments passed in list style
489 31 my $pos = 1;
490 31 ▶ for (my $i=0; $i<scalar(@list_checks); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @list_checks) 44 31 75
491 44 ▶ if (defined $list_checks[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $list_checks[$i]) 28 16 44
492 $str_code .= sprintf q{
493 _croak "%s number %s of $%s fails its constraint: ".((defined $args[0])?$args[0]:"undef") if (!_run($%s[%s], $args[0]));
494 },
495 ($state eq 'before') ? 'input argument' : 'return value',
496 $pos,
497 $varnames->{contractor},
498 $varnames->{list_check},
499 28 ▶ $i;
Branch: 1 branch
BranchTrueFalseTotal
$state eq 'before' ? : 20 8 28
500 }
501  
502 44 $str_code .= q{
503 shift @args;
504 };
505 44 $pos++;
506 }
507  
508 # add code validating trailing arguments passed in hash style
509 31 ▶ if ($validator->has_hash_args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($validator->has_hash_args) 12 19 31
510  
511 # croak if odd number of elements
512 $str_code .= sprintf q{
513 _croak "odd number of hash-style %s in $%s" if (scalar @args %% 2);
514 my %%args = @args;
515 },
516 ($state eq 'before') ? 'input arguments' : 'return values',
517 12 ▶ $varnames->{contractor};
Branch: 1 branch
BranchTrueFalseTotal
$state eq 'before' ? : 8 4 12
518  
519 # check the value of each key in the argument hash
520 12 ▶ while (my ($key,$check) = each %hash_checks) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %hash_checks) 24 12 36
521 24 ▶ if (defined $check) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $check) 16 8 24
522 $str_code .= sprintf q{
523 _croak "%s of $%s with key \'%s\' fails its constraint: %s = ".((defined $args{%s})?$args{%s}:"undef") if (!_run($%s{%s}, $args{%s}));
524 },
525 ($state eq 'before') ? 'input argument' : 'return value',
526 $varnames->{contractor},
527 $key,
528 $key,
529 $key,
530 $key,
531 $varnames->{hash_check},
532 16 ▶ $key,
Branch: 1 branch
BranchTrueFalseTotal
$state eq 'before' ? : 12 4 16
533 $key;
534 }
535  
536 24 $str_code .= sprintf q{
537 delete $args{%s};
538 }, $key;
539 }
540 }
541 }
542  
543 # there should be no arguments left
544 51 ▶ if ($validator->has_hash_args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($validator->has_hash_args) 12 39 51
545 $str_code .= sprintf q{
546 _croak "$%s %s: ".join(" ",keys %%args) if (%%args);
547 },
548 $varnames->{contractor},
549 12 ▶ ($state eq 'before') ? 'got unexpected hash-style input arguments' : 'returned unexpected hash-style return values';
Branch: 1 branch
BranchTrueFalseTotal
$state eq 'before' ? : 8 4 12
550 }
551 }
552  
553 112 return $str_code;
554 }
555  
556 23 1;
557  
558 __END__
« Contract/Memoizer.pm ↑ Summary Contract.pm »