blib/lib/Getargs/Long.pm

filter: Statementstmt 91.0% 204 / 224 Branchbran 77.1% 125 / 162 Conditioncond 76.6% 36 / 47 MC/DCmcdc 44.8% 13 / 29 Subroutinesub 90.0% 18 / 20 Podpod 100% 4 / 4 totaltotal 82.3% 400 / 486 CC 100 SCAR 44.2
CC
100
Cov
84%
_getargs
106.5
_spot_dups
30.0
cgetargs
6.0
CRAP 83.1
↑ Summary
1 # -*- Mode: perl -*-
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use 5.005;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package Getargs::Long;
7  
8 5 ▶use vars qw($VERSION @ISA @EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 $VERSION = sprintf "%d.%02d%02d", q/1.10.11/ =~ /(\d+)/g;
10  
11 BEGIN
12 {
13 5 ▶ die "This module is known to exercise a bug in 5.6.0. Please upgrade your perl.\n"
Subroutine BEGIN: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if $] eq "5.006" 0 5 5
14 if $] eq '5.006';
15 }
16  
17 5 ▶use Log::Agent;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 5 require Exporter;
21 5 ▶use vars qw(@EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 @ISA = qw(Exporter);
23 5 @EXPORT = qw(getargs cgetargs xgetargs cxgetargs);
24  
25 #
26 # %ignore
27 #
28 # Cache whether argument names are to be handled case-insensitively or not,
29 # on a package basis. Default is case-sensitive processing.
30 #
31 5 my %ignore = ();
32  
33 #
34 # ->import
35 #
36 # Trap Exporter's one to handle 'ignorecase' here (or lack thereof).
37 # Then use ->export_to_level() to tell Exporter to continue the export
38 # as if its import method had been called directly via inheritance.
39 #
40 sub import {
41 6 ▶ my $module = shift;
Subroutine import: called CC=2 SCAR=6.9
42 6 my @syms = grep($_ ne 'ignorecase', @_);
43 6 my $callpkg = caller;
44  
45 6 ▶ if (@syms == @_) { # There was no "ignorecase" seen
Branch: 1 branch
BranchTrueFalseTotal
if (@syms == @_) { } 3 3 6
46 3 delete $ignore{$callpkg};
47 3 logdbg 'info', "will process arguments case-sensitively in $callpkg";
48 } else {
49 3 $ignore{$callpkg} = 1;
50 3 logdbg 'info', "will process arguments case-insensitively in $callpkg";
51 }
52  
53 6 Getargs::Long->export_to_level(1, $module, @syms);
54 }
55  
56 #
57 # %subcache
58 #
59 # Cache validation routine, indexed by "package::routine".
60 #
61 5 my %subcache = ();
62  
63 #
64 # getargs
65 #
66 # Parse arguments for subroutine, and validate them if typechecking requested.
67 # Optional arguments with no default return undef. Mandatory arguments cannot
68 # be undefined.
69 #
70 9 ▶sub getargs (\@@) { _getargs(scalar(caller), 0, "", @_) } ## no critic (ProhibitSubroutinePrototypes)
Subroutine getargs: called CC=1 SCAR=0.0
Pod documented
71  
72 #
73 # cgetargs
74 #
75 # Same as getargs, but cache data for next call.
76 #
77 # When called from within an eval, caching is not possible, so this routine
78 # must not be called.
79 #
80 sub cgetargs (\@@) { ## no critic (ProhibitSubroutinePrototypes)
81 0 ▶ my $sub = (caller(1))[3]; # Anomaly in caller(), will also get pkg name
Subroutine cgetargs: not called CC=2 SCAR=17.9
Pod documented
82 0 ▶ logcroak "can't call cgetargs from within an eval"
Branch: 1 branch
BranchTrueFalseTotal
if $sub =~ /^\(eval/ 0 0 0
83 if $sub =~ /^\(eval/;
84 0 _getargs(scalar(caller), 0, $sub, @_)
85 }
86  
87 #
88 # xgetargs
89 #
90 # Like getargs(), but with extended specifications allowing to specify
91 # defaults for non-mandatory arguments.
92 #
93 8 ▶sub xgetargs (\@@) { _getargs(scalar(caller), 1, "", @_) } ## no critic (ProhibitSubroutinePrototypes)
Subroutine xgetargs: called CC=1 SCAR=0.0
Pod documented
94  
95 #
96 # cxgetargs
97 #
98 # Like cgetargs(), but with extended specifications allowing to specify
99 # defaults for non-mandatory arguments. Be careful: those defaults are
100 # deep-cloned and "frozen", so to speak.
101 #
102 # When called from within an eval, caching is not possible, so this routine
103 # must not be called.
104 #
105 sub cxgetargs (\@@) { ## no critic (ProhibitSubroutinePrototypes)
106 8 ▶ my $sub = (caller(1))[3]; # Anomaly in caller(), will also get pkg name
Subroutine cxgetargs: called CC=2 SCAR=7.1
Pod documented
107 8 ▶ logcroak "can't call cxgetargs from within an eval"
Branch: 1 branch
BranchTrueFalseTotal
if $sub =~ /^\(eval/ 0 8 8
108 if $sub =~ /^\(eval/;
109 8 _getargs(scalar(caller), 1, $sub, @_)
110 }
111  
112 #
113 # _getargs
114 #
115 # Factorized work for *getargs() routines
116 #
117 # Our signature is:
118 #
119 # _getargs(
120 # # arguments added by our wrappers
121 # $callpkg, $extended, $subname,
122 # # argument list to parse
123 # \@x,
124 # # optional switches
125 # {
126 # -strict => 1, # unknown switches are fatal
127 # -inplace => 1, # edit \@x inplace: remove parsed args
128 # -ignorecase => 1, # override package's global
129 # -extra => 0, # suppress return of extra arguments
130 # },
131 # # argument definition list
132 # <variable>
133 # );
134 #
135 # With:
136 # $callpkg Calling package
137 # $extended Are they using x*getargs()?
138 # $subname Cache key, if we use it
139 #
140 # Returns the list of values in the same order given in the definition list
141 # (the <variable> part), followed by the extra arguments we did not recognize,
142 # with leading '-' removal and transformation to lowercase if ignorecase is on.
143 #
144 sub _getargs {
145 25 ▶ my ($callpkg, $extended, $subname, $args) = splice(@_, 0, 4);
Subroutine _getargs: called CC=91 SCAR=46.7
146  
147 25 ▶ logconfess "first argument must be a reference to the argument list"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq "ARRAY" 0 25 25
148 unless ref $args eq 'ARRAY';
149  
150 #
151 # Check cache if told to do so.
152 #
153  
154 25 ▶ if ($subname ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($subname ne "") 8 17 25
155 8 my $sref = $subcache{$subname};
156 8 ▶ if (defined $sref) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sref) { } 6 2 8
157 6 logdbg 'info', "calling cached subroutine $sref";
158 6 return &$sref($args);
159 } else {
160 2 logdbg 'info', "no cached subroutine yet for $subname";
161 }
162 }
163  
164 #
165 # Nothing in cache, or cache was disabled.
166 #
167  
168 19 ▶ my $case_insensitive = $ignore{$callpkg} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ignore{$callpkg} ? : 11 8 19
169 19 logdbg 'info', "case_insensitive=$case_insensitive for package $callpkg";
170  
171 #
172 # If next argument is a HASH, then it's a set of extra switches that
173 # may alter our behaviour. Parse them manually.
174 #
175 # Following are the defaults:
176 #
177  
178 19 my $strict = 1; # Fatal error on unknown switches
179 19 my $inplace = 0; # No inplace editing of arguments
180 19 my $extra; # Don't return extra args by default
181  
182 19 ▶ if (ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "HASH") 8 11 19
183 8 my $swref = shift;
184  
185 8 my %set = (
186 -strict => \$strict,
187 -ignorecase => \$case_insensitive,
188 -inplace => \$inplace,
189 -extra => \$extra,
190 );
191  
192 8 ▶ while (my ($sw, $val) = each %$swref) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$swref) 19 8 27
193 19 my $vset = $set{lc($sw)};
194 19 ▶ logcroak "unknown switch $sw" unless ref $vset;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $vset 0 19 19
195 19 $$vset = $val;
196 }
197  
198 #
199 # If they did not set -extra, compute suitable default: false
200 # when -strict, true otherwise.
201 #
202  
203 8 ▶ $extra = $strict ? 0 : 1 unless defined $extra;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $extra 3 5 8
$strict ? : 0 3 3
204  
205 #
206 # If strict, we ignore true settings for -inplace and -extra
207 #
208  
209 8 ▶ if ($strict) {
Branch: 1 branch
BranchTrueFalseTotal
if ($strict) 0 8 8
210 0 ▶ if ($inplace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inplace) 0 0 0
211 0 logcarp "ignoring -inplace when -strict";
212 0 $inplace = 0;
213 }
214 0 ▶ if ($extra) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extra) 0 0 0
215 0 logcarp "ignoring -extra when -strict";
216 0 $extra = 0;
217 }
218 }
219 }
220  
221 #
222 # If we have one argument, it may be '[list]' or 'x'.
223 # In extended mode, we must have an even amount of arguments.
224 #
225  
226 19 my @specs; # User specification list
227 19 my $all_optional = 0; # True if all arguments are optional
228  
229 19 ▶ if (@_ == 1 && ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0]) { } 3 16 19
Condition66%2/3
@_ == 1 and ref $_[0]
!ll&&!rl&&r
1603
MC/DC50%1/2
@_ == 1 and ref $_[0]
@_ == 1 ref $_[0]
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0]
ABresult
0X0
100
111
230 3 ▶ logcroak "must use an array reference for optional args"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "ARRAY" 0 3 3
231 unless ref $_[0] eq 'ARRAY';
232 3 @specs = @{$_[0]};
233 3 $all_optional = 1;
234 } else {
235 16 @specs = @_;
236 16 ▶ logcroak "must supply an even amount of arguments in extend mode"
Branch: 1 branch
BranchTrueFalseTotal
if $extended and @specs % 2 0 16 16
Condition66%2/3
$extended and @specs % 2
!ll&&!rl&&r
6100
MC/DC0%0/2
$extended and @specs % 2
$extended @specs % 2
Truth table66%2/3
A and B
A= $extendedB= @specs % 2
ABresult
0X0
100
111
237 if $extended && (@specs % 2);
238 }
239  
240 #
241 # Parse our argument list and compile it into @args
242 #
243  
244 19 my %seen;
245 my @args; # List of [name, type, is_optional, default]
246  
247 19 ▶ for (my $i = 0, my $step = $extended ? 2 : 1; $i < @specs; $i += $step) {
Branch: 2 branches
BranchTrueFalseTotal
$extended ? : 10 9 19
if ($i < @specs) 80 19 99
248 80 my $arg = $specs[$i];
249 80 my ($name, $type, $optional, $dflt);
250 80 ▶ if ($extended) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extended) { } 50 30 80
251 50 $name = $arg;
252 50 my $spec = $specs[$i+1];
253 50 ▶ if (ref $spec) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $spec) { } 35 15 50
254 # Given as an array ref -> optional, with possible default
255 35 ▶ logcroak "specs for optional '$name' are $spec, expected ARRAY"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $spec eq "ARRAY" 0 35 35
256 unless ref $spec eq 'ARRAY';
257 35 ($type, $dflt) = @$spec;
258 35 $optional = 1;
259 } else {
260 # simple scalar is type, argument is mandatory
261 15 $type = $spec;
262 15 $optional = 0;
263 }
264 } else {
265 # Can be either "name" or "name=Type"
266 30 ($name, $type) = $arg =~ /^-?(\w+)=(\S+)/;
267 30 ▶ $name = $arg unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 12 18 30
268 30 $optional = $all_optional;
269 }
270  
271 80 ▶ $name = lc($name) if $case_insensitive;
Branch: 1 branch
BranchTrueFalseTotal
if $case_insensitive 54 26 80
272 80 $name =~ s/^-//;
273  
274 80 ▶ logcroak "argument name cannot be empty" if $name eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "" 0 80 80
275 80 ▶ logcroak "argument name must be scalar, not $name" if ref $name;
Branch: 1 branch
BranchTrueFalseTotal
if ref $name 0 80 80
276 80 ▶ logcroak "duplicate argument definition for '$name'" if $seen{$name}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$name}++ 0 80 80
277  
278 80 ▶ push(@args, [
Branch: 2 branches
BranchTrueFalseTotal
defined $type ? : 53 27 80
defined $dflt ? : 20 60 80
279 $name,
280 defined($type) ? $type : undef,
281 $optional,
282 defined($dflt) ? $dflt : undef
283 ]);
284 }
285  
286 #
287 # If caching, generate the subroutine that will perform the checks.
288 #
289 # We use logxcroak to report errors to the caller of the caller
290 # of *getargs, i.e. the caller of the routine for which we're checking
291 # the arguments.
292 #
293  
294 19 ▶ if ($subname ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($subname ne "") 2 17 19
295 2 ▶ my $lc = $case_insensitive ? 'lc' : '';
Branch: 1 branch
BranchTrueFalseTotal
$case_insensitive ? : 2 0 2
296 2 my $sub = &_q(<<'EOS');
297 :sub {
298 : my $aref_orig = shift;
299 : my @result;
300 : my $cur;
301 : my $isthere;
302 : my $ctype;
303 : local $Getargs::Long::dflt;
304 : my $i = 0;
305 EOS
306 2 $sub .= &_q(<<EOS);
307 : logxcroak 3, "expected an even number of arguments" if \@\$aref_orig % 2;
308 :
309 : my \%args = map {
310 : (\$i++ % 2) ? \$_ : $lc(/^-/ ? substr(\$_, 1) : \$_) } \@\$aref_orig;
311 EOS
312  
313 # Sanity check: no argument can be given twice
314 2 $sub .= &_q(<<EOS);
315 : _spot_dups(\$aref_orig, $case_insensitive, 3)
316 : if 2 * scalar(keys \%args) != \@\$aref_orig;
317 :
318 EOS
319 # Work on a copy if extra and no inplace
320 2 ▶ if ($extra && !$inplace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extra and not $inplace) { } 0 2 2
Condition33%1/3
$extra and not $inplace
!ll&&!rl&&r
200
MC/DC0%0/2
$extra and not $inplace
$extra not $inplace
Truth table33%1/3
A and not(B)
A= $extraB= not $inplace
ABresult
0X0
100
111
321 0 $sub .= &_q(<<'EOS');
322 : my $aref = [@$aref_orig];
323 EOS
324 } else {
325 2 $sub .= &_q(<<'EOS');
326 : my $aref = $aref_orig;
327 EOS
328 }
329  
330 # Index arguments if inplace editing or extra
331 2 ▶ if ($inplace || $extra) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inplace or $extra) 1 1 2
Condition66%2/3
$inplace or $extra
l!l&&r!l&&!r
101
MC/DC50%1/2
$inplace or $extra
$inplace $extra
Truth table66%2/3
A or B
A= $inplaceB= $extra
ABresult
1X1
011
000
332 1 $sub .= &_q(<<'EOS');
333 :
334 : my $idx;
335 : my %idx;
336 : for (my $j = 0; $j < @$aref; $j += 2) {
337 : my $key = $aref->[$j];
338 : $key =~ s/^-//;
339 EOS
340 1 ▶ $sub .= &_q(<<'EOS') if $case_insensitive;
Branch: 1 branch
BranchTrueFalseTotal
if $case_insensitive 1 0 1
341 : $key = lc($key);
342 EOS
343 1 $sub .= &_q(<<'EOS');
344 : $idx{$key} = $j;
345 : }
346 :
347 EOS
348 }
349  
350 2 foreach my $arg (@args) {
351 10 my ($name, $type, $optional, $dflt) = @$arg;
352 10 my $has_default = defined $dflt;
353 10 local $^W = 0; # Shut up Test::Harness
354 10 $sub .= &_q(<<EOS);
355 : # Argument [name=$name, type=$type, optional=$optional, dflt=$dflt]
356 : \$cur = undef;
357 : \$isthere = 0;
358 : if (exists \$args{$name}) {
359 : \$isthere = 1;
360 : my \$val = delete \$args{$name};
361 : \$cur = \\\$val;
362 EOS
363 10 ▶ $sub .= &_q(<<EOS) if $inplace || $extra;
Branch: 1 branch
BranchTrueFalseTotal
if $inplace or $extra 5 5 10
Condition66%2/3
$inplace or $extra
l!l&&r!l&&!r
505
MC/DC50%1/2
$inplace or $extra
$inplace $extra
Truth table66%2/3
A or B
A= $inplaceB= $extra
ABresult
1X1
011
000
364 : # Splice argument out
365 : \$idx = \$idx{$name};
366 EOS
367 10 ▶ $sub .= &_q(<<'EOS') if $inplace || $extra;
Branch: 1 branch
BranchTrueFalseTotal
if $inplace or $extra 5 5 10
Condition66%2/3
$inplace or $extra
l!l&&r!l&&!r
505
MC/DC50%1/2
$inplace or $extra
$inplace $extra
Truth table66%2/3
A or B
A= $inplaceB= $extra
ABresult
1X1
011
000
368 : splice(@$aref, $idx, 2);
369 : while (my ($k, $v) = each %idx) {
370 : $idx{$k} -= 2 if $v > $idx;
371 : }
372 EOS
373 10 $sub .= &_q(<<'EOS');
374 : }
375 EOS
376 10 ▶ if ($optional) {
Branch: 1 branch
BranchTrueFalseTotal
if ($optional) { } 7 3 10
377 7 ▶ if ($has_default) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_default) 4 3 7
378 4 $sub .= &_q(<<EOS);
379 : else {
380 : eval {
381 : package Getargs::Long::_;
382 : no strict;
383 : \$Getargs::Long::dflt =
384 EOS
385 4 my $obj = Data::Dumper->new([$dflt], []);
386 4 $obj->Purity(1);
387 4 $sub .= $obj->Dumpxs;
388 4 $sub .= &_q(<<'EOS');
389 : };
390 : $cur = \$Getargs::Long::dflt;
391 : }
392 EOS
393 }
394 } else {
395 3 $sub .= &_q(<<EOS);
396 : logxcroak 3, "mandatory argument '$name' missing" unless \$isthere;
397 EOS
398 }
399 10 ▶ if ($type ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type ne "") 7 3 10
400 7 ▶ if ($optional) {
Branch: 1 branch
BranchTrueFalseTotal
if ($optional) { } 5 2 7
401 5 $sub .= &_q(<<EOS);
402 : logxcroak 3, "argument '$name' cannot be undef"
403 : if \$isthere && !defined \$\$cur;
404 EOS
405 } else {
406 2 $sub .= &_q(<<EOS);
407 : logxcroak 3, "argument '$name' cannot be undef" unless defined \$\$cur;
408 EOS
409 }
410 7 ▶ my $opt_is_there = $optional ? "\$isthere &&" : "";
Branch: 1 branch
BranchTrueFalseTotal
$optional ? : 5 2 7
411 7 ▶ if ($type =~ /^[isn]$/) { # Make sure it's a scalar
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /^[isn]$/) { } 3 4 7
412 # XXX Check that i is integer, s string and n natural
413 3 $sub .= &_q(<<EOS);
414 : logxcroak 3,
415 : "argument '$name' must be scalar (type '$type') but is \$\$cur"
416 : if $opt_is_there ref \$\$cur;
417 EOS
418 } else {
419 4 $sub .= &_q(<<EOS);
420 : \$ctype = \$isthere ? ref \$\$cur : undef;
421 : logxcroak 3, "argument '$name' must be of type $type but is \$ctype"
422 : if $opt_is_there (UNIVERSAL::isa(\$\$cur, 'UNIVERSAL') ?
423 : !\$\$cur->isa('$type') :
424 : \$ctype ne '$type');
425 EOS
426 }
427 }
428 10 $sub .= &_q(<<'EOS');
429 : push(@result, defined($cur) ? $$cur : undef);
430 :
431 EOS
432 }
433  
434 # If we're strict, we must report unprocessed switches
435 2 ▶ $sub .= &_q(<<'EOS') if $strict;
Branch: 1 branch
BranchTrueFalseTotal
if $strict 1 1 2
436 :
437 : _spot_unknown(\%args, 3) if scalar keys %args;
438 :
439 EOS
440  
441 # Add extra unprocessed switches to the result list
442 2 ▶ $sub .= &_q(<<'EOS') if $extra;
Branch: 1 branch
BranchTrueFalseTotal
if $extra 0 2 2
443 : push(@result, @$aref);
444 EOS
445 2 $sub .= &_q(<<'EOS');
446 : return @result;
447 :}
448 EOS
449 2 logdbg 'debug', "anonymous subroutine: $sub";
450 2 ▶ my $code = eval $sub; ## no critic (ProhibitStringyEval)
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
451 2 ▶ if (chop($@)) {
Branch: 1 branch
BranchTrueFalseTotal
if (chop $@) { } 0 2 2
452 0 logerr "can't create subroutine for checking args of $subname: $@";
453 0 logwarn "ignoring caching directive for $subname";
454 } else {
455 2 $subcache{$subname} = $code;
456 2 logdbg 'info', "calling newly built subroutine $code";
457 2 return &$code($args);
458 }
459 }
460  
461 #
462 # No caching made, perform validation by interpreting the structure
463 #
464 # There is some unfortunate code duplication between the following checks
465 # and the above routine-construction logic. Some place are identical,
466 # but the main argument processing loop is noticeably different, even
467 # though the same logic is used.
468 #
469  
470 17 logdbg 'info', "interpreting structure to validate arguments";
471  
472 17 my @result;
473 my $cur;
474 17 my $ctype;
475  
476 17 my $i = 0;
477 17 my %args;
478  
479 17 ▶ $args = [@$args] if $extra && !$inplace; # Work on a copy
Branch: 1 branch
BranchTrueFalseTotal
if $extra and not $inplace 3 14 17
Condition66%2/3
$extra and not $inplace
!ll&&!rl&&r
1403
MC/DC50%1/2
$extra and not $inplace
$extra not $inplace
Truth table66%2/3
A and not(B)
A= $extraB= not $inplace
ABresult
0X0
100
111
480  
481 17 ▶ logxcroak 2, "expected an even number of arguments" if @$args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @$args % 2 0 17 17
482  
483 17 ▶ if ($case_insensitive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($case_insensitive) { } 10 7 17
484 10 ▶ %args = map { ($i++ % 2) ? $_ : lc(/^-/ ? substr($_, 1) : $_) } @$args;
Branch: 2 branches
BranchTrueFalseTotal
$i++ % 2 ? : 33 33 66
/^-/ ? : 20 13 33
485 } else {
486 7 ▶ %args = map { ($i++ % 2) ? $_ : (/^-/ ? substr($_, 1) : $_) } @$args;
Branch: 2 branches
BranchTrueFalseTotal
$i++ % 2 ? : 26 26 52
/^-/ ? : 26 0 26
487 }
488  
489 # Sanity check: no argument can be given twice
490 17 ▶ _spot_dups($args, $case_insensitive, 2)
Branch: 1 branch
BranchTrueFalseTotal
if 2 * scalar(keys %args) != @$args 0 17 17
491 if 2 * scalar(keys %args) != @$args;
492  
493 # Index arguments if inplace editing or extra
494 17 my %idx;
495 17 ▶ if ($inplace || $extra) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inplace or $extra) 7 10 17
Condition100%3/3
$inplace or $extra
l!l&&r!l&&!r
4310
MC/DC100%2/2
$inplace or $extra
$inplace $extra
Truth table100%3/3
A or B
A= $inplaceB= $extra
ABresult
1X1
011
000
496 7 ▶ for (my $j = 0; $j < @$args; $j += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @$args) 19 7 26
497 19 my $key = $args->[$j];
498 19 $key =~ s/^-//;
499 19 ▶ $key = lc($key) if $case_insensitive;
Branch: 1 branch
BranchTrueFalseTotal
if $case_insensitive 17 2 19
500 19 $idx{$key} = $j;
501 }
502 }
503  
504 # Process each argument
505 17 foreach my $arg (@args) {
506 68 my ($name, $type, $optional, $dflt) = @$arg;
507 68 my $cur;
508 68 my $isthere = 0;
509 68 ▶ if (exists $args{$name}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $args{$name}) { } 50 18 68
elsif ($optional) { } 15 3 18
510 50 $isthere = 1;
511 50 my $val = delete $args{$name};
512 50 $cur = \$val;
513  
514 # Splice argument out if requested
515 50 ▶ if ($inplace || $extra) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inplace or $extra) 17 33 50
Condition100%3/3
$inplace or $extra
l!l&&r!l&&!r
11633
MC/DC100%2/2
$inplace or $extra
$inplace $extra
Truth table100%3/3
A or B
A= $inplaceB= $extra
ABresult
1X1
011
000
516 17 my $idx = $idx{$name};
517 17 splice(@$args, $idx, 2);
518 17 ▶ while (my ($k, $v) = each %idx) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %idx) 53 17 70
519 53 ▶ $idx{$k} -= 2 if $v > $idx;
Branch: 1 branch
BranchTrueFalseTotal
if $v > $idx 20 33 53
520 }
521 }
522 } elsif ($optional) {
523 15 ▶ $cur = \$dflt if defined $dflt;
Branch: 1 branch
BranchTrueFalseTotal
if defined $dflt 6 9 15
524 } else {
525 3 logxcroak 2, "mandatory argument '$name' missing";
526 }
527  
528 65 ▶ push(@result, defined($cur) ? $$cur : undef);
Branch: 1 branch
BranchTrueFalseTotal
defined $cur ? : 56 9 65
529 65 ▶ next if !defined $type || $type eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $type or $type eq "" 23 42 65
Condition66%2/3
not defined $type or $type eq ""
l!l&&r!l&&!r
23042
MC/DC50%1/2
not defined $type or $type eq ""
not defined $type $type eq ""
Truth table66%2/3
not(A) or B
A= not defined $typeB= $type eq ""
ABresult
1X1
011
000
530  
531 42 ▶ if ($optional) {
Branch: 1 branch
BranchTrueFalseTotal
if ($optional) { } 29 13 42
532 29 ▶ logxcroak 2, "argument '$name' cannot be undef"
Branch: 1 branch
BranchTrueFalseTotal
if $isthere and not defined $$cur 0 29 29
Condition66%2/3
$isthere and not defined $$cur
!ll&&!rl&&r
9200
MC/DC0%0/2
$isthere and not defined $$cur
$isthere not defined $$cur
Truth table66%2/3
A and not(B)
A= $isthereB= not defined $$cur
ABresult
0X0
100
111
533 if $isthere && !defined $$cur;
534 } else {
535 13 ▶ logxcroak 2,
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$cur 1 12 13
536 "argument '$name' cannot be undef" unless defined $$cur;
537 }
538  
539 # XXX Check that i is integer, s string and n natural
540 41 ▶ if ($type =~ /^[isn]$/) { # Make sure it's a scalar
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /^[isn]$/) { } 16 25 41
541 16 ▶ logxcroak 2,
Branch: 1 branch
BranchTrueFalseTotal
if !$optional || $isthere and ref $$cur 0 16 16
Condition83%5/6
!$optional || $isthere and ref $$cur
!ll&&!rl&&r
3130
not $optional or $isthere
l!l&&r!l&&!r
583
MC/DC0%0/3
!$optional || $isthere and ref $$cur
not $optional $isthere ref $$cur
Truth table60%3/5
not(A) or B and C
A= not $optionalB= $isthereC= ref $$cur
ABCresult
00X0
1X00
0100
1X11
0111
542 "argument '$name' must be scalar (type '$type') but is $$cur"
543 if (!$optional || $isthere) && ref $$cur;
544 } else {
545 25 ▶ my $ctype = $isthere ? ref $$cur : undef;
Branch: 1 branch
BranchTrueFalseTotal
$isthere ? : 19 6 25
546 25 ▶ logxcroak 2, "argument '$name' must be of type $type but is $ctype"
Branch: 2 branches
BranchTrueFalseTotal
if !$optional || $isthere and UNIVERSAL::isa($$cur, "UNIVERSAL") ? !$$cur->isa($type) : $ctype ne $type 1 24 25
UNIVERSAL::isa($$cur, 'UNIVERSAL') ? : 9 10 19
Condition100%6/6
!$optional || $isthere and UNIVERSAL::isa($$cur, "UNIVERSAL") ? !$$cur->isa($type) : $ctype ne $type
!ll&&!rl&&r
6181
not $optional or $isthere
l!l&&r!l&&!r
7126
MC/DC66%2/3
!$optional || $isthere and UNIVERSAL::isa($$cur, "UNIVERSAL") ? !$$cur->isa($type) : $ctype ne $type
not $optional $isthere UNIVERSAL::isa($$cur, "UNIVERSAL") ? !$$cur->isa($type) : $ctype ne $type
Truth table80%4/5
not(A) or B and C
A= not $optionalB= $isthereC= UNIVERSAL::isa($$cur, "UNIVERSAL") ? !$$cur->isa($type) : $ctype ne $type
ABCresult
00X0
1X00
0100
1X11
0111
547 if (!$optional || $isthere) &&
548 (UNIVERSAL::isa($$cur, 'UNIVERSAL') ?
549 !$$cur->isa($type) :
550 $ctype ne $type);
551 }
552 }
553  
554 # If we're strict, we must report unprocessed switches
555 12 ▶ _spot_unknown(\%args, 2) if $strict && scalar keys %args;
Branch: 1 branch
BranchTrueFalseTotal
if $strict and scalar keys %args 4 8 12
Condition100%2/2
$strict and scalar keys %args
!ll
57
MC/DC100%1/1
$strict and scalar keys %args
$strict
Truth table100%2/2
A
A= $strict
Aresult
00
11
556  
557 # Add extra unprocessed switches to the result list
558 8 ▶ push(@result, @$args) if $extra;
Branch: 1 branch
BranchTrueFalseTotal
if $extra 3 5 8
559  
560 8 return @result;
561 }
562  
563 #
564 # _spot_dups
565 #
566 # Given a list of arguments in $aref, where we know there are duplicate "keys",
567 # identify them and croak by listing the culprits.
568 #
569 sub _spot_dups {
570 0 ▶ my ($aref, $ignorecase, $level) = @_;
Subroutine _spot_dups: not called CC=5 SCAR=34.0
571 0 my %seen;
572 my @duplicates;
573 0 ▶ for (my $i = 0; $i < @$aref; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$aref) 0 0 0
574 0 ▶ my $key = $ignorecase ? lc($aref->[$i]) : $aref->[$i];
Branch: 1 branch
BranchTrueFalseTotal
$ignorecase ? : 0 0 0
575 0 $key =~ s/^-//;
576 0 ▶ push(@duplicates, "-$key") if $seen{$key}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$key}++ 0 0 0
577 }
578 0 ▶ logconfess "bug in Getargs::Long -- should have found duplicates"
Branch: 1 branch
BranchTrueFalseTotal
unless @duplicates 0 0 0
579 unless @duplicates;
580 0 logxcroak ++$level,
581 "multiple switches given for: " . join(", ", @duplicates);
582 }
583  
584 #
585 # _spot_unknown
586 #
587 # Report keys held in supplied hashref as unknown switches.
588 #
589 sub _spot_unknown {
590 5 ▶ my ($href, $level) = @_;
Subroutine _spot_unknown: called CC=2 SCAR=7.0
591 5 my @unprocessed = map { "-$_" } keys %$href;
592 5 ▶ my $es = @unprocessed == 1 ? '' : 'es';
Branch: 1 branch
BranchTrueFalseTotal
@unprocessed == 1 ? : 5 0 5
593 5 logxcroak ++$level, "unknown switch$es: " . join(", ", @unprocessed);
594 }
595  
596 sub _q {
597 79 ▶ local $_ = shift;
Subroutine _q: called CC=1 SCAR=0.0
598 79 s/^://gm;
599 79 return $_;
600 }
601  
602 5 1;
603  
604 __END__
↑ Summary