lib/TUI/toolkit/Params.pm

filter: Statementstmt 88.9% 402 / 452 Branchbran 72.2% 156 / 216 Conditioncond 65.1% 58 / 89 MC/DCmcdc 38.7% 19 / 49 Subroutinesub 84.7% 50 / 59 Podpod 0.0% 0 / 2 totaltotal 79.0% 685 / 867 CC 153 SCAR 28.4
CC
153
Cov
81%
_expand_specs
39.1
__ANON__
30.0
__ANON__
29.6
CRAP 17.1
« lib/TUI/Views/Scroller.pm ↑ Summary lib/TUI/Validate/RangeValidator.pm »
1 package TUI::toolkit::Params;
2 # ABSTRACT: Lightweight signature validation inspired by Type::Params
3
4 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7
8 3 our $VERSION = '0.10';
9 3 our $AUTHORITY = 'cpan:BRICKPOOL';
10
11 # ----------------------------------------------------------------------
12 # Imports
13 # ----------------------------------------------------------------------
14
15 3 ▶use B::Deparse ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Params::Check ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: not called CC=1 SCAR=0.0
19
20 10 ▶BEGIN { sub HAVE_SUB_UTIL () { eval q[ use Sub::Util () ]; !$@ } }
Subroutine HAVE_SUB_UTIL: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Pod undocumented
21
22 # ----------------------------------------------------------------------
23 # Exports
24 # ----------------------------------------------------------------------
25
26 3 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
27
28 3 our @EXPORT_OK = qw(
29 signature
30 );
31
32 # ----------------------------------------------------------------------
33 # Define vars, types, templates, ...
34 # ----------------------------------------------------------------------
35
36 3 our @CARP_NOT = ( __PACKAGE__ );
37
38 # built-in Type checker
39 2 ▶my $is_Undef = sub ($) { !defined($_[0]) };
Subroutine __ANON__: called CC=1 SCAR=0.0
40 6 ▶my $is_Str = sub ($) { defined($_[0]) && ref(\$_[0]) eq 'SCALAR' };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and ref \$_[0] eq "SCALAR" 4 2 6
41 33 ▶my $is_Bool = sub ($) { !defined($_[0]) || $_[0] =~ /\A[01]?\z/ };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
not defined $_[0] or $_[0] =~ /\A[01]?\z/ 0 33 33
42 0 ▶my $is_Int = sub ($) { defined($_[0]) && $_[0] =~ /\A-?\d+\z/ };
Subroutine __ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and $_[0] =~ /\A-?\d+\z/ 0 0 0
43 0 ▶my $is_Object = sub ($) { Scalar::Util::blessed($_[0]) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
44 14 ▶my $is_Ref = sub ($) { !!ref($_[0]) };
Subroutine __ANON__: called CC=1 SCAR=0.0
45 0 ▶my $is_ScalarRef = sub ($) { ref($_[0]) eq 'SCALAR' || ref($_[0]) eq 'REF' };
Subroutine __ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq "SCALAR" or ref $_[0] eq "REF" 0 0 0
46 46 ▶my $is_ArrayRef = sub ($) { ref($_[0]) eq 'ARRAY' };
Subroutine __ANON__: called CC=1 SCAR=0.0
47 0 ▶my $is_CodeRef = sub ($) { ref($_[0]) eq 'CODE' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
48
49 # syntax templates
50 3 my $tmpl_spec = {
51 pos => {
52 allow => $is_ArrayRef,
53 },
54 positional => {
55 allow => $is_ArrayRef,
56 },
57 named => {
58 allow => $is_ArrayRef,
59 },
60 named_to_list => { no_override => 1, default => 0 },
61 list_to_named => { no_override => 1, default => 0 },
62 head => { no_override => 1 },
63 tail => { no_override => 1 },
64 method => {
65 allow => [ $is_Bool, $is_Object, $is_CodeRef ],
66 },
67 description => {
68 allow => $is_Str,
69 },
70 package => {
71 allow => $is_Str,
72 },
73 fallback => { no_override => 1 },
74 on_die => { no_override => 1 },
75 strictness => {
76 default => 1,
77 allow => $is_Bool,
78 },
79 multiple => { no_override => 1 },
80 message => {
81 allow => $is_Str,
82 },
83 bless => { no_override => 1, default => 1 },
84 class => { no_override => 1 },
85 constructor => { no_override => 1 },
86 returns => { no_override => 1 },
87 returns_scalar => { no_override => 1 },
88 returns_list => { no_override => 1 },
89 allow_dash => {
90 default => 0,
91 allow => $is_Bool,
92 },
93 subname => {
94 allow => $is_Str,
95 },
96 caller_level => {
97 default => 0,
98 allow => $is_Int,
99 },
100 next => { no_override => 1 },
101 want_source => { no_override => 1, default => 0 },
102 want_details => { no_override => 1, default => 0 },
103 want_object => { no_override => 1, default => 0 },
104 };
105
106 3 my $tmpl_param = {
107 optional => {
108 default => 0,
109 allow => $is_Bool,
110 },
111 slurpy => {
112 default => 0,
113 allow => $is_Bool,
114 },
115 default => {
116 allow => [ $is_Ref, $is_Str, $is_Undef ],
117 },
118 default_on_undef => { no_override => 1, default => 0 },
119 coerce => { no_override => 1, default => 0 },
120 clone => { no_override => 1, default => 0 },
121 name => { no_override => 1 },
122 getter => { no_override => 1 },
123 predicate => { no_override => 1 },
124 alias => {
125 allow => [ $is_Str, $is_ArrayRef ],
126 },
127 in_list => { no_override => 1, default => 0 },
128 strictness => { no_override => 1, default => 1 },
129 };
130
131 sub _croak ($);
132
133 # ----------------------------------------------------------------------
134 # Public API
135 # ----------------------------------------------------------------------
136
137 #
138 # Build-time constructor for a positional parameter validator.
139 #
140 # This function performs the following steps:
141 #
142 # 1. Parse the raw specification into a Signature structure. The Signature
143 # contains a Parameter structure for all fixed or named parameters and
144 # optionally one slurpy parameter.
145 #
146 # 2. Precompute all static metadata needed for efficient runtime
147 # execution.
148 #
149 # 3. Pre-generate type-check closures for each parameter. These closures
150 # perform the actual runtime validation of individual values.
151 #
152 # 4. Pre-generate the slurpy checker (if present), which validates
153 # the arrayref of remaining arguments.
154 #
155 # 5. Returns the final runtime executor closure. The executor performs:
156 # - arity validation
157 # - type checking
158 # - default and optional handling
159 # - alias handling
160 # - slurpy collection
161 #
162 # The returned closure is the actual validator that will be invoked
163 # for each call site. Most expensive work is done at build time.
164 #
165 46 ▶sub signature {
Subroutine signature: called CC=1 SCAR=0.0
Pod undocumented
166 46 my $norm = _validate_specs( @_ );
167 45 my $parsed = _expand_specs( $norm );
168 38 my $compiled = _execute_signature( $parsed );
169 38 return $compiled;
170 }
171
172 # ----------------------------------------------------------------------
173 # Parser
174 # ----------------------------------------------------------------------
175
176 # Validate and normalize top level specifications
177 # - Param: %spec hash including the raw specs
178 # - Returns: a validated and normalized signature hash ref
179 46 ▶sub _validate_specs { # \%signature (%spec)
Subroutine _validate_specs: called CC=11 SCAR=24.2
180 46 local $Params::Check::PRESERVE_CASE = 1;
181 46 local $Params::Check::STRIP_LEADING_DASHES = 0;
182 46 local $Params::Check::ALLOW_UNKNOWN = 0;
183
184 # Pre-Validate the spec
185 46 my $spec = { @_ };
186 46 for ( keys %$spec ) {
187 49 ▶ Carp::croak "Unknown parameter '$_'" if not exists $tmpl_spec->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $tmpl_spec->{$_} 1 48 49
188 }
189
190 45 ▶ $spec = Params::Check::check( $tmpl_spec, $spec )
Branch: 1 branch
BranchTrueFalseTotal
$spec = Params::Check::check($tmpl_spec, $spec) or Carp::croak(Params::Check::last_error()) 45 0 45
191 or Carp::croak Params::Check::last_error();
192
193 # Check mutually exclusive pos/positional/named/multiple
194 45 ▶ grep( exists $spec->{$_}, qw( pos positional named multiple ) ) == 1
Branch: 1 branch
BranchTrueFalseTotal
grep(exists $spec->{$_}, ("pos", "positional", "named", "multiple")) == 1 or Carp::croak("The Specification must contain exactly one of the options: pos, positional, named, or multiple") 45 0 45
195 or Carp::croak "The Specification must contain exactly one of the ".
196 "options: pos, positional, named, or multiple";
197
198 # Extract parts
199 45 my $caller_level = $spec->{caller_level};
200 45 my ( $package, undef, undef, $subname ) = caller( $caller_level + 2 );
201 45 my $method = $spec->{method};
202 45 ▶ my $pos = $spec->{pos} || $spec->{positional} || [];
Condition100%5/5
$spec->{'pos'} || $spec->{'positional'} || []
l!l
414
$spec->{"pos"} or $spec->{"positional"}
l!l&&r!l&&!r
3744
MC/DC100%2/2
$spec->{'pos'} || $spec->{'positional'} || []
$spec->{"pos"} $spec->{"positional"}
Truth table100%3/3
A or B
A= $spec->{"pos"}B= $spec->{"positional"}
ABresult
1X1
011
000
203 45 ▶ my $named = $spec->{named} || [];
Condition100%2/2
$spec->{'named'} || []
l!l
441
MC/DC100%1/1
$spec->{'named'} || []
$spec->{'named'}
Truth table100%2/2
A
A= $spec->{'named'}
Aresult
11
00
204
205 # Init result signature hash
206 my $sig = {
207 package => $package,
208 subname => $subname,
209 method => $method,
210 parameters => [],
211 strictness => $spec->{strictness} ? 1 : 0,
212 is_named => @$named ? 1 : 0,
213 45 ▶ allow_dash => $spec->{allow_dash} ? 1 : 0,
Branch: 3 branches
BranchTrueFalseTotal
$spec->{'strictness'} ? : 45 0 45
@$named ? : 4 41 45
$spec->{'allow_dash'} ? : 0 45 45
214 pos => $pos,
215 named => $named,
216 caller_depth => $caller_level,
217 };
218
219 45 return $sig;
220 }
221
222 # Expand raw spec into param specs
223 # - Param: \%signature structure including the normalized specs
224 # - Returns: a signature hash ref representing the parsed specs
225 45 ▶sub _expand_specs { # \%signature (\%signature)
Subroutine _expand_specs: called CC=38 SCAR=36.7
226 45 my ( $sig ) = @_;
227
228 # Extract parts
229 45 my $method = $sig->{method};
230 45 my $pos = $sig->{pos};
231 45 my $named = $sig->{named};
232 45 my $is_named = $sig->{is_named};
233
234 # flags and helpers
235 45 my $has_method = 0;
236 45 my $has_optional = 0;
237 45 my $has_slurpy = 0;
238
239 45 my @params;
240 my $slurpy;
241 45 my %alias_of;
242
243 # Method handling: build method param if given
244 45 ▶ if ( $method ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method) 3 42 45
245 my $isa = ref $sig->{method}
246 ? $sig->{method}
247 3 ▶ : sub { defined $_[0] };
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
ref $sig->{'method'} ? : 0 3 3
248
249 # pre-compile method checker
250 3 my $type_check = _generate_type_checker( $isa );
251 5 ▶ my $method_check = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
252 5 my ( $val ) = @_;
253 5 return $type_check->( $val, '$_[0]' );
254 3 };
255
256 3 $method = {
257 isa => $isa,
258 optional => 0,
259 name => '$_[0]',
260 alias => [],
261 check => $method_check,
262 };
263
264 3 $has_method = 1;
265 }
266
267 #
268 # Parse named parameters
269 # Format: named => [ name => $type, { options }, name2 => $type2, ... ]
270 # options may contain: optional, default, alias, slurpy
271 #
272 45 ▶ if ( $is_named ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_named) { } 4 41 45
273
274 4 my $i = 0;
275 4 ▶ while ( $i < @$named ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$named) 6 4 10
276 6 my $name = $named->[ $i++ ];
277 6 my $type = $named->[ $i++ ];
278 6 my $label = sprintf '$_{"%s"}', $name;
279
280 6 my $opts = {};
281 6 ▶ if ( $i < @$named && ref $named->[$i] eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$named and ref $named->[$i] eq "HASH") 4 2 6
Condition66%2/3
$i < @$named and ref $named->[$i] eq "HASH"
!ll&&!rl&&r
024
MC/DC50%1/2
$i < @$named and ref $named->[$i] eq "HASH"
$i < @$named ref $named->[$i] eq "HASH"
Truth table66%2/3
A and B
A= $i < @$namedB= ref $named->[$i] eq "HASH"
ABresult
0X0
100
111
282 4 $opts = $named->[ $i++ ];
283 }
284
285 # validate opts
286 6 for ( keys %$opts ) {
287 4 ▶ Carp::croak "Unknown parameter '$_'" unless exists $tmpl_param->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $tmpl_param->{$_} 0 4 4
288 }
289 6 ▶ $opts = Params::Check::check( $tmpl_param, $opts )
Branch: 1 branch
BranchTrueFalseTotal
$opts = Params::Check::check($tmpl_param, $opts) or Carp::croak(Params::Check::last_error()) 6 0 6
290 or Carp::croak Params::Check::last_error();
291
292 # normalize default (if present)
293 6 my $default;
294 6 ▶ if ( exists $opts->{default} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"default"}) 1 5 6
295 1 $default = _materialize_default( $opts->{default} );
296 1 $opts->{optional} = 1; # default implies optional
297 }
298
299 # slurpy handling
300 6 ▶ if ( $opts->{slurpy} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"slurpy"}) 1 5 6
301 Carp::croak "Slurpy parameter cannot be optional"
302 1 ▶ if $opts->{optional};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"optional"} 0 1 1
303 1 ▶ Carp::croak "Slurpy parameter must not appear more than once"
Branch: 1 branch
BranchTrueFalseTotal
if $has_slurpy 0 1 1
304 if $has_slurpy;
305
306 1 $has_slurpy = 1;
307
308 # pre-compile slurpy checker
309 1 my $type_check = _generate_type_checker( $type );
310 1 ▶ my $slurpy_check = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
311 1 my ( $val ) = @_;
312 1 return $type_check->( $val, $label );
313 1 };
314
315 1 $slurpy = {
316 isa => $type,
317 optional => 1,
318 name => $name,
319 alias => [],
320 check => $slurpy_check,
321 is_hash => 1, # for named slurpy we always treat as hash
322 };
323
324 1 next; # do not push into params
325 }
326
327 # optionals flag
328 # $has_optional = 1 if $opts->{optional};
329
330 # alias handling
331 5 my @aliases = ();
332 5 ▶ if ( defined $opts->{alias} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{"alias"}) 1 4 5
333 my $aref = ref $opts->{alias} eq 'ARRAY'
334 ? $opts->{alias}
335 1 ▶ : [ $opts->{alias} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $opts->{'alias'} eq 'ARRAY' ? : 1 0 1
336
337 1 foreach my $a ( @$aref ) {
338 Carp::croak "Alias '$a' duplicates existing parameter"
339 if exists $alias_of{$a}
340 0 ▶ || grep { $_->{name} eq $a } @params;
Branch: 1 branch
BranchTrueFalseTotal
if exists $alias_of{$a} or grep {$_->{"name"} eq $a;} @params 0 2 2
Condition33%1/3
exists $alias_of{$a} or grep {$_->{"name"} eq $a;} @params
l!l&&r!l&&!r
002
MC/DC0%0/2
exists $alias_of{$a} or grep {$_->{"name"} eq $a;} @params
exists $alias_of{$a} grep {$_->{"name"} eq $a;} @params
Truth table33%1/3
A or B
A= exists $alias_of{$a}B= grep {$_->{"name"} eq $a;} @params
ABresult
1X1
011
000
341
342 2 $alias_of{$a} = $name;
343 2 push @aliases => $a;
344 }
345 }
346
347 # pre-compile checkers for named parameters
348 5 my $type_check = _generate_type_checker( $type );
349 7 ▶ my $param_check = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
350 7 my ( $val ) = @_;
351 7 return $type_check->( $val, $label );
352 5 };
353
354 # build param hash
355 my $param = {
356 isa => $type,
357 5 ▶ optional => $opts->{optional} ? 1 : 0,
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'optional'} ? : 1 4 5
358 name => $name,
359 alias => \@aliases,
360 check => $param_check,
361 };
362 5 ▶ $param->{default} = $default if exists $opts->{default};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"default"} 1 4 5
363
364 5 push @params => $param;
365 }
366 }
367
368 #
369 # Parse positional parameters
370 # Format: pos/positional => [ $type, { options }, $type2, ... ]
371 # options may contain: optional, default, slurpy
372 #
373 else {
374
375 41 ▶ my $offset = $has_method ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$has_method ? : 2 39 41
376 41 my $i = 0;
377 41 ▶ while ( $i < @$pos ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$pos) 72 34 106
378 72 my $name = sprintf '$_[%d]', @params + $offset;
379 72 my $type = $pos->[ $i++ ];
380
381 72 my $opts = {};
382 72 ▶ if ( $i < @$pos && ref( $pos->[$i] ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$pos and ref $pos->[$i] eq "HASH") 41 31 72
Condition100%3/3
$i < @$pos and ref $pos->[$i] eq "HASH"
!ll&&!rl&&r
92241
MC/DC100%2/2
$i < @$pos and ref $pos->[$i] eq "HASH"
$i < @$pos ref $pos->[$i] eq "HASH"
Truth table100%3/3
A and B
A= $i < @$posB= ref $pos->[$i] eq "HASH"
ABresult
0X0
100
111
383 41 $opts = $pos->[ $i++ ];
384 }
385
386 # validate opts
387 72 for ( keys %$opts ) {
388 41 ▶ Carp::croak "Unknown parameter '$_'" unless exists $tmpl_param->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $tmpl_param->{$_} 0 41 41
389 }
390 72 ▶ $opts = Params::Check::check( $tmpl_param, $opts )
Branch: 1 branch
BranchTrueFalseTotal
$opts = Params::Check::check($tmpl_param, $opts) or Carp::croak(Params::Check::last_error()) 72 0 72
391 or Carp::croak Params::Check::last_error();
392
393 # normalize default (if present)
394 72 my $default;
395 72 ▶ if ( exists $opts->{default} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"default"}) 13 59 72
396 13 $default = _materialize_default( $opts->{default} );
397 12 $opts->{optional} = 1; # default implies optional
398 }
399
400 # slurpy handling
401 71 ▶ if ( $opts->{slurpy} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"slurpy"}) 13 58 71
402 Carp::croak "Slurpy parameter cannot be optional"
403 13 ▶ if $opts->{optional};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"optional"} 0 13 13
404 13 ▶ Carp::croak "Parameter following slurpy parameter"
Branch: 1 branch
BranchTrueFalseTotal
if $i < @$pos 4 9 13
405 if $i < @$pos;
406
407 9 $has_slurpy = 1;
408
409 # pre-compile slurpy checker
410 9 my $type_check = _generate_type_checker( $type );
411 10 ▶ my $slurpy_check = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
412 10 my ( $val ) = @_;
413 10 return $type_check->( $val, '$SLURPY' );
414 9 };
415
416 9 ▶ my $is_hash = _detect_slurpy_is_hash( $type ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
_detect_slurpy_is_hash($type) ? : 3 6 9
417
418 9 $slurpy = {
419 isa => $type,
420 optional => 0,
421 name => '$SLURPY',
422 alias => [],
423 check => $slurpy_check,
424 is_hash => $is_hash,
425 };
426
427 9 next;
428 }
429
430 # optional-block rule
431 58 ▶ if ( $opts->{optional} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'optional'}) { } 25 33 58
elsif ($has_optional) { } 2 31 33
432 25 $has_optional = 1;
433 }
434 elsif ( $has_optional ) {
435 2 Carp::croak "Non-Optional parameter following Optional parameter";
436 }
437
438 # alias not really supported for pos
439 56 ▶ if ( defined $opts->{alias} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{"alias"}) 0 56 56
440 0 Carp::carp "alias not supported for positional parameters";
441 }
442
443 # pre-compile checkers for fixed parameters
444 56 my $type_check = _generate_type_checker( $type );
445 94 ▶ my $param_check = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
446 94 my ( $val ) = @_;
447 94 return $type_check->( $val, $name );
448 56 };
449
450 # build param hash
451 my $param = {
452 isa => $type,
453 56 ▶ optional => $opts->{optional} ? 1 : 0,
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'optional'} ? : 25 31 56
454 name => $name,
455 alias => [],
456 check => $param_check,
457 };
458 56 ▶ $param->{default} = $default if exists $opts->{default};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"default"} 12 44 56
459
460 56 push @params => $param;
461 }
462 }
463
464 # finalize $sig
465 38 $sig->{method} = $method;
466 38 $sig->{parameters} = \@params;
467 38 $sig->{slurpy} = $slurpy;
468
469 38 return $sig;
470 }
471
472 # ----------------------------------------------------------------------
473 # Executor
474 # ----------------------------------------------------------------------
475
476 # Constructs the runtime executor for a compiled signature.
477 # - Params: \%signature with precompiled checker
478 # - Returns: a code ref to validate the arguments -> sub (@_)
479 38 ▶sub _execute_signature { # \&executor (\%signature)
Subroutine _execute_signature: called CC=10 SCAR=24.0
480 38 my ( $sig ) = @_;
481
482 # Flags for optional and alias
483 38 my $has_optional = !! grep { $_->{optional} } @{ $sig->{parameters} };
484 38 my $has_aliases = !! grep { @{ $_->{alias} } } @{ $sig->{parameters} };
485
486 # generate named executor
487 38 ▶ if ( $sig->{is_named} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig->{"is_named"}) 4 34 38
488 return _executor_named_hot( $sig )
489 if !defined $sig->{slurpy}
490 && !$sig->{allow_dash}
491 && !$sig->{strictness}
492 4 ▶ && !$has_optional
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sig->{"slurpy"} or $sig->{"allow_dash"} or $sig->{"strictness"} or $has_optional or $has_aliases 0 4 4
Condition50%6/12
defined $sig->{"slurpy"} or $sig->{"allow_dash"} or $sig->{"strictness"} or $has_optional or $has_aliases
l!l&&r!l&&!r
400
defined $sig->{"slurpy"} or $sig->{"allow_dash"} or $sig->{"strictness"} or $has_optional
l!l&&r!l&&!r
400
defined $sig->{"slurpy"} or $sig->{"allow_dash"} or $sig->{"strictness"}
l!l&&r!l&&!r
130
defined $sig->{"slurpy"} or $sig->{"allow_dash"}
l!l&&r!l&&!r
103
MC/DC0%0/5
defined $sig->{"slurpy"} or $sig->{"allow_dash"} or $sig->{"strictness"} or $has_optional or $has_aliases
defined $sig->{"slurpy"} $sig->{"allow_dash"} $sig->{"strictness"} $has_optional $has_aliases
Truth table33%2/6
A or B or C or D or E
A= defined $sig->{"slurpy"}B= $sig->{"allow_dash"}C= $sig->{"strictness"}D= $has_optionalE= $has_aliases
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
493 && !$has_aliases;
494
495 4 return _executor_named_cold( $sig );
496 }
497
498 # generate positional executor
499 return _executor_pos_hot( $sig )
500 if defined $sig->{method}
501 && !defined $sig->{slurpy}
502 34 ▶ && !$has_optional;
Branch: 1 branch
BranchTrueFalseTotal
if defined $sig->{"method"} and not defined $sig->{"slurpy"} and not $has_optional 1 33 34
Condition83%5/6
defined $sig->{"method"} and not defined $sig->{"slurpy"} and not $has_optional
!ll&&!rl&&r
3211
defined $sig->{"method"} and not defined $sig->{"slurpy"}
!ll&&!rl&&r
3202
MC/DC66%2/3
defined $sig->{"method"} and not defined $sig->{"slurpy"} and not $has_optional
defined $sig->{"method"} not defined $sig->{"slurpy"} not $has_optional
Truth table75%3/4
A and not(B) and not(C)
A= defined $sig->{"method"}B= not defined $sig->{"slurpy"}C= not $has_optional
ABCresult
0XX0
10X0
1100
1111
503
504 33 return _executor_pos_cold( $sig );
505 }
506
507 # Fast path for simple named signatures: NO slurpy, NO alias, BUT method
508 # - Params: \%signature with precompiled checker
509 # - Returns: coderef of a pre compiled named parameter checker
510 0 ▶sub _executor_named_hot { # \&executor (\%signature)
Subroutine _executor_named_hot: not called CC=1 SCAR=6.9
511 0 my ( $sig ) = @_;
512
513 # Precompute checkers and required names
514 0 my $method_check = $sig->{method}{check};
515 0 my $method_name = $sig->{method}{name};
516 0 my %check_for = map { $_->{name} => $_->{check} } @{ $sig->{parameters} };
517 0 my @required = sort keys %check_for;
518
519 0 ▶ return sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
520 0 local $Carp::CarpLevel = $Carp::CarpLevel + $sig->{caller_depth} + 1;
521
522 0 my $proto = shift;
523 0 $method_check->( $proto, $method_name );
524
525 0 ▶ _croak "Odd number of named parameters" unless @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ % 2 0 0 0
526
527 0 my %args = @_;
528
529 0 for my $name ( @required ) {
530 0 ▶ exists $args{$name}
Branch: 1 branch
BranchTrueFalseTotal
exists $args{$name} or &_croak("Missing required named parameter '${name}'") 0 0 0
531 or _croak "Missing required named parameter '$name'";
532
533 0 ▶ eval { $check_for{$name}->( $args{$name}, $name ); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $check_for{$name}->($args{$name}, $name); 1 } } or &_croak($@) 0 0 0
534 or _croak( $@ );
535 }
536
537 0 return ( $proto, \%args );
538 0 };
539 }
540
541 # Handles all named cases (slurpy, alias, method, ...)
542 # - Params: \%signature with precompiled checker
543 # - Returns: coderef of a pre compiled named parameter checker
544 4 ▶sub _executor_named_cold { # \&executor (\%signature)
Subroutine _executor_named_cold: called CC=3 SCAR=11.4
545 4 my ( $sig ) = @_;
546
547 # Extract specs from signature
548 4 my @params = @{ $sig->{parameters} };
549 4 my $method = $sig->{method};
550 4 my $slurpy = $sig->{slurpy};
551
552 4 my %alias_of;
553 4 for my $p ( @params ) {
554 5 $alias_of{$_} = $p->{name} for @{ $p->{alias} };
555 }
556
557 # Flags
558 4 my $has_method = defined $method;
559 4 my $has_slurpy = defined $slurpy;
560 4 my $strict = $sig->{strictness};
561 4 my $allow_dash = $sig->{allow_dash};
562
563 8 ▶ return sub {
Subroutine __ANON__: called CC=21 SCAR=33.9
564 # Adjust Carp stack level so errors point to the caller of the signature
565 8 local $Carp::CarpLevel = $Carp::CarpLevel + $sig->{caller_depth} + 1;
566
567 8 my $argc = @_;
568
569 # Optional method handling
570 8 my $self;
571 8 ▶ if ( $has_method ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_method) 2 6 8
572 2 $self = shift;
573 2 ▶ eval { $method->{check}->( $self, $method->{name} ); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $method->{"check"}->($self, $method->{"name"}); 1 } } or &_croak($@) 1 1 2
574 or _croak( $@ );
575 1 $argc--;
576 }
577
578 # Quick even-number check
579 7 ▶ _croak("Odd number of named parameters") if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 7 7
580
581 # Strip leading dash (optional)
582 7 my %raw;
583 7 ▶ if ( $allow_dash ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_dash) { } 0 7 7
584 0 ▶ while ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
585 0 my ( $key, $val ) = ( shift, shift );
586 0 $key =~ s/^-//;
587 0 $raw{$key} = $val;
588 }
589 }
590 else {
591 7 %raw = @_;
592 }
593
594 # Apply alias mapping: alias_of => main
595 7 my %args = %raw;
596 7 for my $alias ( keys %alias_of ) {
597 8 ▶ next unless exists $raw{$alias};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $raw{$alias} 5 3 8
598
599 3 my $main = $alias_of{$alias};
600
601 3 ▶ if ( exists $raw{$main} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $raw{$main}) 1 2 3
602 1 _croak "Superfluous alias \"$alias\" for argument \"$main\"";
603 }
604
605 # move alias value to main key
606 2 $args{$main} = delete $args{$alias};
607 }
608
609 # Fixed parameter processing: required/optional/default
610 6 my %out; # name -> value
611 my %used; # keys consumed by fixed params
612 6 for my $p ( @params ) {
613 7 my $name = $p->{name};
614 7 my $val;
615
616 # argument provided?
617 7 ▶ if ( exists $args{$name} ) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $args{$name}) { } 6 1 7
elsif (ref $p->{'default'}) { } 1 0 1
elsif ($p->{'optional'}) { } 0 0 0
618 6 $val = $args{$name};
619 6 $used{$name} = 1;
620 }
621
622 # default provided?
623 elsif ( ref $p->{default} ) {
624 1 $val = $p->{default}->( $self )
625 }
626
627 # optional without default
628 elsif ( $p->{optional} ) {
629 next
630 0 }
631
632 else {
633 0 _croak( "Missing required named parameter '$name'" );
634 }
635
636 # Run type check
637 7 ▶ eval { $p->{check}->( $val, $name ); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $p->{"check"}->($val, $name); 1 } } or &_croak($@) 7 0 7
638 or _croak( $@ );
639 7 $out{$name} = $val;
640 }
641
642 # Slurpy or unknown-key handling
643 6 my %rest = map { $_ => $args{$_} } grep { !$used{$_} } keys %args;
644
645 6 ▶ if ( $has_slurpy ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_slurpy) { } 1 5 6
646 1 my $val;
647 1 ▶ if ( $slurpy->{is_hash} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slurpy->{'is_hash'}) { } 1 0 1
648 # Hash-style slurpy: pass remaining key/value pairs as hashref
649 1 $val = \%rest;
650 }
651 else {
652 # Fallback: array-style slurpy for named (rare case)
653 0 my @pairs;
654 0 for my $key ( keys %rest ) {
655 0 push @pairs, $key, $rest{$key};
656 }
657 0 $val = \@pairs;
658 }
659 1 ▶ eval { $slurpy->{check}->( $val, $slurpy->{name} ); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $slurpy->{"check"}->($val, $slurpy->{"name"}); 1 } } or &_croak($@) 1 0 1
660 or _croak( $@ );
661
662 1 $out{ $slurpy->{name} } = $val;
663 }
664 else {
665 # No slurpy and strictness: detect unknown keys
666 5 ▶ if ( $strict && %rest ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($strict and %rest) 0 5 5
Condition33%1/3
$strict and %rest
!ll&&!rl&&r
050
MC/DC0%0/2
$strict and %rest
$strict %rest
Truth table33%1/3
A and B
A= $strictB= %rest
ABresult
0X0
100
111
667 0 my @unknown = sort keys %rest;
668 0 _croak( "Unknown named parameter(s): " . join( ", ", @unknown ) );
669 }
670 }
671
672 6 ▶ return ( $self, \%out ) if $has_method;
Branch: 1 branch
BranchTrueFalseTotal
if $has_method 1 5 6
673 5 return \%out;
674 4 };
675 }
676
677 # Fast path for simple positional signatures: NO method, NO optional, NO slurpy
678 # - Params: \%signature with precompiled checker
679 # - Returns: coderef of a pre compiled positional parameter checker
680 1 ▶sub _executor_pos_hot { # \&executor (\%signature)
Subroutine _executor_pos_hot: called CC=2 SCAR=7.2
681 1 my ( $sig ) = @_;
682
683 # Precompute arity and checkers
684 1 my $arity = scalar @{ $sig->{parameters} };
685 0 my @check_for = map { $_->{check} } @{ $sig->{parameters} };
686
687 # Optional method at position 0
688 1 ▶ if ( my $method = $sig->{method} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = $sig->{"method"}) 1 0 1
689 1 $arity++;
690 1 unshift @check_for => $method->{check};
691 }
692
693 1 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=15.0
694 1 local $Carp::CarpLevel = $Carp::CarpLevel + $sig->{caller_depth} + 1;
695
696 # Fixed arity check
697 1 ▶ if ( @_ != $arity ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ != $arity) 0 1 1
698 0 my $argc = @_;
699 0 _croak "Wrong number of parameters; got $argc; expected $arity";
700 }
701
702 # Validate all arguments in-place
703 1 for my $i ( 0 .. $#check_for ) {
704 1 ▶ eval { $check_for[$i]->( $_[$i], "\$_[$i]" ); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $check_for[$i]->($_[$i], "\$_[$i]"); 1 } } or &_croak($@) 1 0 1
705 or _croak( $@ );
706 }
707
708 # Hot path: return @_ unchanged
709 1 return @_;
710 1 };
711 }
712
713 # Handles all positional cases (method, optional, slurpy ...)
714 # - Params: \%signature with precompiled checker
715 # - Returns: coderef of a pre compiled positional parameter checker
716 33 ▶sub _executor_pos_cold { # \&executor (\%signature)
Subroutine _executor_pos_cold: called CC=2 SCAR=6.9
717 33 my ( $sig ) = @_;
718
719 # Extract specs from signature
720 33 my @params = @{ $sig->{parameters} };
721 33 my $method = $sig->{method};
722 33 my $slurpy = $sig->{slurpy};
723
724 # Flags
725 33 my $has_method = defined $method;
726 33 my $has_slurpy = defined $slurpy;
727
728 # Precompute arity
729 33 ▶ my $offset = $has_method ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$has_method ? : 1 32 33
730 33 my $min_arity = $offset + scalar grep { !$_->{optional} } @params;
731 33 my $max_arity = $offset + scalar @params;
732
733 72 ▶ return sub {
Subroutine __ANON__: called CC=21 SCAR=30.5
734 72 local $Carp::CarpLevel = $Carp::CarpLevel + $sig->{caller_depth} + 1;
735
736 72 my $argc = @_;
737
738 # Arity checks
739 72 my $too_few = $argc < $min_arity;
740 72 ▶ my $too_many = !$has_slurpy && $argc > $max_arity;
Condition100%3/3
!$has_slurpy && $argc > $max_arity
!ll&&!rl&&r
12546
MC/DC100%2/2
!$has_slurpy && $argc > $max_arity
!$has_slurpy $argc > $max_arity
Truth table100%3/3
not(A) && B
A= !$has_slurpyB= $argc > $max_arity
ABresult
0X0
100
111
741
742 72 ▶ if ( $too_few || $too_many ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($too_few or $too_many) 17 55 72
Condition100%3/3
$too_few or $too_many
l!l&&r!l&&!r
11655
MC/DC100%2/2
$too_few or $too_many
$too_few $too_many
Truth table100%3/3
A or B
A= $too_fewB= $too_many
ABresult
1X1
011
000
743 17 ▶ my $expected = $has_slurpy ? "at least $min_arity"
Branch: 2 branches
BranchTrueFalseTotal
$has_slurpy ? : 1 16 17
$min_arity == $max_arity ? : 9 7 16
744 : $min_arity == $max_arity ? "$min_arity"
745 : "$min_arity to $max_arity";
746
747 17 _croak "Wrong number of parameters; got $argc; expected $expected";
748 }
749
750 55 my @out;
751
752 # Method handling (optional)
753 my $self;
754 55 ▶ if ( $has_method ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_method) 2 53 55
755 2 $self = shift;
756 2 ▶ eval { $method->{check}->( $self, $method->{name} ); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $method->{"check"}->($self, $method->{"name"}); 1 } } or &_croak($@) 2 0 2
757 or _croak( $@ );
758 2 $argc--;
759 2 push @out, $self;
760 }
761
762 # Validate fixed (non-slurpy) positional arguments
763 55 for my $i ( 0 .. $#params ) {
764 102 my $p = $params[$i];
765 102 my $name = $p->{name};
766 102 my $val;
767
768 # argument provided?
769 102 ▶ if ( $i < $argc ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($i < $argc) { } 82 20 102
elsif (ref $p->{'default'}) { } 12 8 20
elsif ($p->{'optional'}) { } 8 0 8
770 82 $val = $_[$i];
771 }
772
773 # default provided?
774 elsif ( ref $p->{default} ) {
775 12 $val = $p->{default}->( $self )
776 }
777
778 # optional without default -> stop, no further checks
779 elsif ( $p->{optional} ) {
780 8 last;
781 }
782
783 # Run type check
784 94 ▶ eval { $p->{check}->( $val, $name ); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $p->{"check"}->($val, $name); 1 } } or &_croak($@) 87 7 94
785 or _croak( $@ );
786 87 push @out, $val;
787 }
788
789 # If there is no slurpy parameter, just return
790 48 ▶ return @out unless $has_slurpy;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_slurpy 37 11 48
791
792 # Slurpy: arrayref or hashref of remaining args (empty is allowed)
793 11 my $rest = [];
794 11 my $slurpy_ofs = scalar @params;
795 11 my $slurpy_len = $argc - $slurpy_ofs;
796
797 11 ▶ if ( $slurpy_len > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slurpy_len > 0) 10 1 11
798 10 my $slurpy_is_hash = $slurpy->{is_hash};
799
800 # Special case: number of remaining parameters is 1
801 10 ▶ if ( $slurpy_len == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slurpy_len == 1) { } 4 6 10
802 4 my $last = $_[-1];
803 4 ▶ if ( !$slurpy_is_hash ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $slurpy_is_hash) { } 2 2 4
elsif (ref $last ne 'HASH') { } 0 2 2
804 2 $rest = [$last];
805 }
806 elsif ( ref $last ne 'HASH' ) {
807 0 $rest = {$last};
808 }
809 else {
810 2 $rest = $last;
811 }
812 }
813
814 # Default case: process remaining parameters
815 else {
816 6 my @slurpy_args = @_[ $slurpy_ofs .. $slurpy_ofs + $slurpy_len - 1 ];
817 6 ▶ if ( $slurpy_is_hash ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slurpy_is_hash) { } 3 3 6
818 3 ▶ _croak "Odd number of elements in slurpy hash parameter"
Branch: 1 branch
BranchTrueFalseTotal
if @slurpy_args % 2 0 3 3
819 if @slurpy_args % 2;
820 3 $rest = {@slurpy_args};
821 }
822 else {
823 3 $rest = \@slurpy_args;
824 }
825 }
826
827 # Check remaining parameters in a single step
828 10 ▶ eval { $slurpy->{check}->( $rest, $slurpy->{name} ); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $slurpy->{"check"}->($rest, $slurpy->{"name"}); 1 } } or &_croak($@) 7 3 10
829 or _croak( $@ );
830 }
831
832 # Return fixed values plus array ref for slurpy
833 8 return ( @out, $rest );
834 33 };
835
836 }
837
838 # ----------------------------------------------------------------------
839 # Utilities
840 # ----------------------------------------------------------------------
841
842 # Generate a checker subroutine for a single type.
843 # - Param: $isa as a type object or CODE reference
844 # - Returns: a code reference for on signature-entry -> sub ($value, $label)
845 74 ▶sub _generate_type_checker { # \&check ($isa)
Subroutine _generate_type_checker: called CC=13 SCAR=30.0
846 74 my ( $isa ) = @_;
847
848 # 1. Type::API::Constraint::Inlinable (e.g. Type::Standard)
849 74 ▶ if ( Scalar::Util::blessed( $isa )
Branch: 4 branches
BranchTrueFalseTotal
if (builtin::blessed($isa) and $isa->DOES('Type::API::Constraint::Inlinable') and $isa->can_be_inlined) { } 59 15 74
elsif (builtin::blessed($isa) and $isa->DOES('Type::API::Constraint')) { } 5 10 15
elsif (builtin::blessed($isa) and $isa->can('check')) { } 0 10 10
elsif (ref $isa eq 'CODE') { } 10 0 10
Condition83%5/6
builtin::blessed($isa) and $isa->DOES("Type::API::Constraint::Inlinable") and $isa->can_be_inlined
!ll&&!rl&&r
10559
builtin::blessed($isa) and $isa->DOES("Type::API::Constraint::Inlinable")
!ll&&!rl&&r
10064
MC/DC66%2/3
builtin::blessed($isa) and $isa->DOES("Type::API::Constraint::Inlinable") and $isa->can_be_inlined
builtin::blessed($isa) $isa->DOES("Type::API::Constraint::Inlinable") $isa->can_be_inlined
Truth table75%3/4
A and B and C
A= builtin::blessed($isa)B= $isa->DOES("Type::API::Constraint::Inlinable")C= $isa->can_be_inlined
ABCresult
0XX0
10X0
1100
1111
850 && $isa->DOES( "Type::API::Constraint::Inlinable" )
851 && $isa->can_be_inlined
852 ) {
853 # Ask the type for inline code; it may use $val, $_[1], or local $_
854 59 my $inline = $isa->inline_check( '$val' );
855
856 # Build the low-level predicate: ($isa, $value) -> bool
857 59 my $check = do {
858 59 local $@;
859 59 ▶ eval "sub { my (\$isa, \$val) = \@_; $inline; }"
Condition50%1/2
eval "sub { my (\$isa, \$val) = \@_; $inline; }" || Carp::croak("Error compiling inline predicate: $@")
l!l
590
MC/DC0%0/1
eval "sub { my (\$isa, \$val) = \@_; $inline; }" || Carp::croak("Error compiling inline predicate: $@")
eval "sub { my (\$isa, \$val) = \@_; $inline; }"
Truth table50%1/2
A
A= eval "sub { my (\$isa, \$val) = \@_; $inline; }"
Aresult
11
00
860 or Carp::croak "Error compiling inline predicate: $@";
861 };
862
863 # High-level checker: ($value, $label) -> $value or croak
864 94 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
865 94 my ( $val, $label ) = @_;
866 94 local $_ = $val;
867
868 94 ▶ return $val if $isa->$check( $val );
Branch: 1 branch
BranchTrueFalseTotal
if $isa->$check($val) 86 8 94
869 8 die( $isa->get_message( $val ) . " (in $label)\n" );
870 59 };
871 }
872
873 # 2. Type::API::Constraint (non-inlinable)
874 ▶ elsif ( Scalar::Util::blessed( $isa )
Condition66%2/3
builtin::blessed($isa) and $isa->DOES("Type::API::Constraint")
!ll&&!rl&&r
1005
MC/DC50%1/2
builtin::blessed($isa) and $isa->DOES("Type::API::Constraint")
builtin::blessed($isa) $isa->DOES("Type::API::Constraint")
Truth table66%2/3
A and B
A= builtin::blessed($isa)B= $isa->DOES("Type::API::Constraint")
ABresult
0X0
100
111
875 && $isa->DOES( "Type::API::Constraint" )
876 ) {
877 8 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
878 8 my ( $val, $label ) = @_;
879 8 local $_ = $val;
880
881 8 ▶ return $val if $isa->check( $val );
Branch: 1 branch
BranchTrueFalseTotal
if $isa->check($val) 6 2 8
882 2 die( $isa->get_message( $val ) . " (in $label)\n" );
883 5 };
884 }
885
886 # 3. Generic Type::API-style object with ->check
887 ▶ elsif ( Scalar::Util::blessed( $isa ) && $isa->can( 'check' ) ) {
Condition33%1/3
builtin::blessed($isa) and $isa->can("check")
!ll&&!rl&&r
1000
MC/DC0%0/2
builtin::blessed($isa) and $isa->can("check")
builtin::blessed($isa) $isa->can("check")
Truth table33%1/3
A and B
A= builtin::blessed($isa)B= $isa->can("check")
ABresult
0X0
100
111
888
889 0 ▶ return sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
890 0 my ( $val, $label ) = @_;
891 0 local $_ = $val;
892
893 0 ▶ return $val if $isa->check( $val );
Branch: 1 branch
BranchTrueFalseTotal
if $isa->check($val) 0 0 0
894
895 0 ▶ my $msg = $isa->can( 'get_message' )
Branch: 1 branch
BranchTrueFalseTotal
$isa->can('get_message') ? : 0 0 0
896 ? $isa->get_message( $val )
897 : "Argument did not pass type constraint";
898
899 0 die( "$msg (in $label)\n" );
900 0 };
901 }
902
903 # 4. plain CODE reference
904 elsif ( ref( $isa ) eq 'CODE' ) {
905 10 my $name = _coderef2text( $isa );
906 10 ▶ if ( HAVE_SUB_UTIL ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&HAVE_SUB_UTIL()) 10 0 10
907 10 my $subname = Sub::Util::subname( $isa );
908 10 ▶ $name = $subname if $subname && $subname !~ /::__ANON__$/;
Branch: 1 branch
BranchTrueFalseTotal
if $subname and not $subname =~ /::__ANON__$/ 0 10 10
Condition33%1/3
$subname and not $subname =~ /::__ANON__$/
!ll&&!rl&&r
0100
MC/DC0%0/2
$subname and not $subname =~ /::__ANON__$/
$subname not $subname =~ /::__ANON__$/
Truth table33%1/3
A and not(B)
A= $subnameB= not $subname =~ /::__ANON__$/
ABresult
0X0
100
111
909 }
910
911 15 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=14.6
912 15 my ( $val, $label ) = @_;
913 15 local $_ = $val;
914
915 15 ▶ return $val if $isa->( $val );
Branch: 1 branch
BranchTrueFalseTotal
if &$isa($val) 14 1 15
916
917 1 ▶ my $desc = !defined( $val ) ? "Undef"
Branch: 2 branches
BranchTrueFalseTotal
!defined($val) ? : 1 0 1
ref $val ? : 0 0 0
918 : ref( $val ) ? "Reference $val"
919 : "Value \"$val\"";
920
921 1 die( "$desc did not pass type constraint $name (in $label)\n" );
922 10 };
923 }
924
925 # 5. Unsupported type specification
926 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
927 0 my ( undef, $label ) = @_;
928 0 die( "Unsupported type definition for argument $label\n" );
929 0 };
930 }
931
932 # Inspects a potential slurpy type and determines whether it behaves like a
933 # HashRef.
934 # - Param: $isa as a type object or CODE reference
935 # - Returns: a boolean value evaluates to true for a hash
936 9 ▶sub _detect_slurpy_is_hash { # $bool ($isa)
Subroutine _detect_slurpy_is_hash: called CC=9 SCAR=24.4
937 9 my ( $isa ) = @_;
938
939 # 1. Type object with ->name that starts with "HashRef"
940 9 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($isa) and $isa->can("name") and $isa->name =~ /^HashRef/ 3 6 9
Condition50%3/6
builtin::blessed($isa) and $isa->can("name") and $isa->name =~ /^HashRef/
!ll&&!rl&&r
063
builtin::blessed($isa) and $isa->can("name")
!ll&&!rl&&r
009
MC/DC33%1/3
builtin::blessed($isa) and $isa->can("name") and $isa->name =~ /^HashRef/
builtin::blessed($isa) $isa->can("name") $isa->name =~ /^HashRef/
Truth table50%2/4
A and B and C
A= builtin::blessed($isa)B= $isa->can("name")C= $isa->name =~ /^HashRef/
ABCresult
0XX0
10X0
1100
1111
941 if Scalar::Util::blessed( $isa )
942 && $isa->can( 'name' )
943 && $isa->name =~ /^HashRef/;
944
945 # 2. Type object with ->check that accepts {} but not []
946 6 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($isa) and $isa->can("check") and $isa->check({}) and not $isa->check([]) 0 6 6
Condition55%5/9
builtin::blessed($isa) and $isa->can("check") and $isa->check({}) and not $isa->check([])
!ll&&!rl&&r
510
builtin::blessed($isa) and $isa->can("check") and $isa->check({})
!ll&&!rl&&r
051
builtin::blessed($isa) and $isa->can("check")
!ll&&!rl&&r
006
MC/DC0%0/4
builtin::blessed($isa) and $isa->can("check") and $isa->check({}) and not $isa->check([])
builtin::blessed($isa) $isa->can("check") $isa->check({}) not $isa->check([])
Truth table40%2/5
A and B and C and not(D)
A= builtin::blessed($isa)B= $isa->can("check")C= $isa->check({})D= not $isa->check([])
ABCDresult
0XXX0
10XX0
110X0
11100
11111
947 if Scalar::Util::blessed( $isa )
948 && $isa->can( 'check' )
949 && $isa->check( {} ) && !$isa->check( [] );
950
951 # 3. Fallback: use $isa as coderef
952 6 local $@;
953 6 ▶ return eval { $isa->( {} ) } && !eval { $isa->( [] ) };
Condition66%2/3
eval { do { &$isa(scalar {}) } } && !eval { do { &$isa(scalar []) } }
!ll&&!rl&&r
510
MC/DC0%0/2
eval { do { &$isa(scalar {}) } } && !eval { do { &$isa(scalar []) } }
eval { do { &$isa(scalar {}) } } !eval { do { &$isa(scalar []) } }
Truth table66%2/3
A && not(B)
A= eval { do { &$isa(scalar {}) } }B= !eval { do { &$isa(scalar []) } }
ABresult
0X0
100
111
954 }
955
956 # Validate and normalize default value (if any)
957 # - Param: the $default option
958 # - Returns: the validated and normalized default -> sub { $self }
959 14 ▶sub _materialize_default { # $default ($default)
Subroutine _materialize_default: called CC=11 SCAR=24.1
960 14 my ( $default ) = @_;
961
962 14 ▶ if ( !defined $default ) {
Branch: 6 branches
BranchTrueFalseTotal
if (not defined $default) { } 2 12 14
elsif (not ref $default) { } 3 9 12
elsif (ref $default eq 'CODE') { } 3 6 9
elsif (ref $default eq 'ARRAY' and @$default == 0) { } 2 4 6
elsif (ref $default eq 'HASH' and not keys %$default) { } 1 3 4
elsif (ref $default eq 'SCALAR' and not ref $$default) { } 2 2 4
963 2 ▶ return sub { undef };
Subroutine __ANON__: called CC=1 SCAR=0.0
964 }
965 elsif ( !ref $default ) {
966 3 ▶ return sub { $default };
Subroutine __ANON__: called CC=1 SCAR=0.0
967 }
968 elsif ( ref $default eq 'CODE' ) {
969 3 return $default;
970 }
971 ▶ elsif ( ref $default eq 'ARRAY' && @$default == 0 ) {
Condition66%2/3
ref $default eq "ARRAY" and @$default == 0
!ll&&!rl&&r
402
MC/DC50%1/2
ref $default eq "ARRAY" and @$default == 0
ref $default eq "ARRAY" @$default == 0
Truth table66%2/3
A and B
A= ref $default eq "ARRAY"B= @$default == 0
ABresult
0X0
100
111
972 2 ▶ return sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
973 }
974 ▶ elsif ( ref $default eq 'HASH' && !keys %$default ) {
Condition66%2/3
ref $default eq "HASH" and not keys %$default
!ll&&!rl&&r
301
MC/DC50%1/2
ref $default eq "HASH" and not keys %$default
ref $default eq "HASH" not keys %$default
Truth table66%2/3
A and not(B)
A= ref $default eq "HASH"B= not keys %$default
ABresult
0X0
100
111
975 1 ▶ return sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
976 }
977 ▶ elsif ( ref $default eq 'SCALAR' && !ref $$default ) {
Condition66%2/3
ref $default eq "SCALAR" and not ref $$default
!ll&&!rl&&r
102
MC/DC50%1/2
ref $default eq "SCALAR" and not ref $$default
ref $default eq "SCALAR" not ref $$default
Truth table66%2/3
A and not(B)
A= ref $default eq "SCALAR"B= not ref $$default
ABresult
0X0
100
111
978 # compile scalar-ref code
979 2 my $src = $$default;
980 2 return do {
981 2 local $@;
982 2 ▶ eval "sub { $src }"
Branch: 1 branch
BranchTrueFalseTotal
eval "sub { $src }" or Carp::croak("Invalid default expression '${src}': $@") 2 0 2
983 or Carp::croak "Invalid default expression '$src': $@";
984 };
985 }
986
987 1 Carp::croak "Default expected to be undef, string, coderef, or empty " .
988 "arrayref/hashref";
989 }
990
991 # Reconstructs the code from Perl's internal syntax tree
992 # - Param: $code is a type constraint CODE reference
993 # - Returns: A string that maps the code reference from Perl's Optree.
994 10 ▶sub _coderef2text {
Subroutine _coderef2text: called CC=2 SCAR=6.9
995 10 my ( $code ) = @_;
996 10 state $DEPARSE = B::Deparse->new( "-P", "-sC" );
997 10 my $body = $DEPARSE->coderef2text( $code );
998 10 for ( $body ) {
999 10 s/^\h+(?:use|no) (?:strict|warnings|feature|integer|utf8|bytes|re)\b[^\n]*\n//gm;
1000 10 s/^\h+package [^\n]*;\n//gm;
1001 10 s/\A\{\n\h+([^\n;]*);\n\}\z/{ $1 }/;
1002 }
1003 10 return $body;
1004 }
1005
1006 # Lightweight Carp-style exception using caller and $Carp::CarpLevel
1007 # - Param: $msg is the error message
1008 # - Effect: Dies with a message tagged with the adjusted caller location
1009 29 ▶sub _croak ($) {
Subroutine _croak: called CC=4 SCAR=14.2
1010 29 ▶ my $error = shift || 'Unknown error';
Condition50%1/2
shift() || 'Unknown error'
l!l
290
MC/DC0%0/1
shift() || 'Unknown error'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1011 29 $error =~ s/\s+at \S+ line \d+\.?\s*$//;
1012 29 my $msg = Carp::shortmess( $error );
1013 29 ▶ unless ( $Carp::Verbose ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Carp::Verbose) 29 0 29
1014 29 my ( undef, $file, $line ) = caller( $Carp::CarpLevel + 1 );
1015 29 ▶ if ( $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) 29 0 29
1016 29 $msg =~ s/\s+at \S+ line \d+\.?\s*$/ at $file line $line.\n/s;
1017 }
1018 }
1019 29 die $msg;
1020 }
1021
1022 3 1
1023
1024 __END__
« lib/TUI/Views/Scroller.pm ↑ Summary lib/TUI/Validate/RangeValidator.pm »