blib/lib/Data/OptList.pm

filter: Statementstmt 100% 64 / 64 Branchbran 97.0% 33 / 34 Conditioncond 75.0% 9 / 12 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 100% 13 / 13 Podpod 100% 2 / 2 totaltotal 93.2% 124 / 133 CC 22 SCAR 26.0
CC
22
Cov
96%
mkopt
21.0
mkopt_hash
2.0
__ANON__
1.0
CRAP 13.4
↑ Summary
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Data::OptList 0.115;
4 # ABSTRACT: parse and validate simple name/value option pairs
5  
6 2 ▶use List::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Params::SomeUtil ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Sub::Install 0.921 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 #pod =head1 SYNOPSIS
11 #pod
12 #pod use Data::OptList;
13 #pod
14 #pod my $options = Data::OptList::mkopt([
15 #pod qw(key1 key2 key3 key4),
16 #pod key5 => { ... },
17 #pod key6 => [ ... ],
18 #pod key7 => sub { ... },
19 #pod key8 => { ... },
20 #pod key8 => [ ... ],
21 #pod ]);
22 #pod
23 #pod ...is the same thing, more or less, as:
24 #pod
25 #pod my $options = [
26 #pod [ key1 => undef, ],
27 #pod [ key2 => undef, ],
28 #pod [ key3 => undef, ],
29 #pod [ key4 => undef, ],
30 #pod [ key5 => { ... }, ],
31 #pod [ key6 => [ ... ], ],
32 #pod [ key7 => sub { ... }, ],
33 #pod [ key8 => { ... }, ],
34 #pod [ key8 => [ ... ], ],
35 #pod ]);
36 #pod
37 #pod =head1 DESCRIPTION
38 #pod
39 #pod Hashes are great for storing named data, but if you want more than one entry
40 #pod for a name, you have to use a list of pairs. Even then, this is really boring
41 #pod to write:
42 #pod
43 #pod $values = [
44 #pod foo => undef,
45 #pod bar => undef,
46 #pod baz => undef,
47 #pod xyz => { ... },
48 #pod ];
49 #pod
50 #pod Just look at all those undefs! Don't worry, we can get rid of those:
51 #pod
52 #pod $values = [
53 #pod map { $_ => undef } qw(foo bar baz),
54 #pod xyz => { ... },
55 #pod ];
56 #pod
57 #pod Aaaauuugh! We've saved a little typing, but now it requires thought to read,
58 #pod and thinking is even worse than typing... and it's got a bug! It looked right,
59 #pod didn't it? Well, the C<< xyz => { ... } >> gets consumed by the map, and we
60 #pod don't get the data we wanted.
61 #pod
62 #pod With Data::OptList, you can do this instead:
63 #pod
64 #pod $values = Data::OptList::mkopt([
65 #pod qw(foo bar baz),
66 #pod xyz => { ... },
67 #pod ]);
68 #pod
69 #pod This works by assuming that any defined scalar is a name and any reference
70 #pod following a name is its value.
71 #pod
72 #pod =func mkopt
73 #pod
74 #pod my $opt_list = Data::OptList::mkopt($input, \%arg);
75 #pod
76 #pod Valid arguments are:
77 #pod
78 #pod moniker - a word used in errors to describe the opt list; encouraged
79 #pod require_unique - if true, no name may appear more than once
80 #pod must_be - types to which opt list values are limited (described below)
81 #pod name_test - a coderef used to test whether a value can be a name
82 #pod (described below, but you probably don't want this)
83 #pod
84 #pod This produces an array of arrays; the inner arrays are name/value pairs.
85 #pod Values will be either "undef" or a reference.
86 #pod
87 #pod Positional parameters may be used for compatibility with the old C<mkopt>
88 #pod interface:
89 #pod
90 #pod my $opt_list = Data::OptList::mkopt($input, $moniker, $req_uni, $must_be);
91 #pod
92 #pod Valid values for C<$input>:
93 #pod
94 #pod undef -> []
95 #pod hashref -> [ [ key1 => value1 ] ... ] # non-ref values become undef
96 #pod arrayref -> every name followed by a non-name becomes a pair: [ name => ref ]
97 #pod every name followed by undef becomes a pair: [ name => undef ]
98 #pod otherwise, it becomes [ name => undef ] like so:
99 #pod [ "a", "b", [ 1, 2 ] ] -> [ [ a => undef ], [ b => [ 1, 2 ] ] ]
100 #pod
101 #pod By default, a I<name> is any defined non-reference. The C<name_test> parameter
102 #pod can be a code ref that tests whether the argument passed it is a name or not.
103 #pod This should be used rarely. Interactions between C<require_unique> and
104 #pod C<name_test> are not yet particularly elegant, as C<require_unique> just tests
105 #pod string equality. B<This may change.>
106 #pod
107 #pod The C<must_be> parameter is either a scalar or array of scalars; it defines
108 #pod what kind(s) of refs may be values. If an invalid value is found, an exception
109 #pod is thrown. If no value is passed for this argument, any reference is valid.
110 #pod If C<must_be> specifies that values must be CODE, HASH, ARRAY, or SCALAR, then
111 #pod Params::SomeUtil is used to check whether the given value can provide that
112 #pod interface. Otherwise, it checks that the given value is an object of the kind.
113 #pod
114 #pod In other words:
115 #pod
116 #pod [ qw(SCALAR HASH Object::Known) ]
117 #pod
118 #pod Means:
119 #pod
120 #pod _SCALAR0($value) or _HASH($value) or _INSTANCE($value, 'Object::Known')
121 #pod
122 #pod =cut
123  
124 2 my %test_for;
125 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
126 2 %test_for = (
127 CODE => \&Params::SomeUtil::_CODELIKE, ## no critic
128 HASH => \&Params::SomeUtil::_HASHLIKE, ## no critic
129 ARRAY => \&Params::SomeUtil::_ARRAYLIKE, ## no critic
130 SCALAR => \&Params::SomeUtil::_SCALAR0, ## no critic
131 );
132 }
133  
134 37 ▶sub mkopt {
Subroutine mkopt: called CC=21 SCAR=30.5
Pod documented
135 37 my ($opt_list) = shift;
136  
137 37 my ($moniker, $require_unique, $must_be); # the old positional args
138 37 my ($name_test, $is_a);
139  
140 37 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 30 7 37
141 30 ▶ if (@_ == 1 and Params::SomeUtil::_HASHLIKE($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and &Params::SomeUtil::_HASHLIKE($_[0])) { } 3 27 30
Condition66%2/3
@_ == 1 and &Params::SomeUtil::_HASHLIKE($_[0])
!ll&&!rl&&r
2703
MC/DC50%1/2
@_ == 1 and &Params::SomeUtil::_HASHLIKE($_[0])
@_ == 1 &Params::SomeUtil::_HASHLIKE($_[0])
Truth table66%2/3
A and B
A= @_ == 1B= &Params::SomeUtil::_HASHLIKE($_[0])
ABresult
0X0
100
111
142 ($moniker, $require_unique, $must_be, $name_test)
143 3 = @{$_[0]}{ qw(moniker require_unique must_be name_test) };
144 } else {
145 27 ($moniker, $require_unique, $must_be) = @_;
146 }
147  
148 # Transform the $must_be specification into a closure $is_a
149 # that will check if a value matches the spec
150  
151 30 ▶ if (defined $must_be) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $must_be) 20 10 30
152 20 ▶ $must_be = [ $must_be ] unless ref $must_be;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $must_be 7 13 20
153 my @checks = map {
154 20 my $class = $_;
155 $test_for{$class}
156 7 ▶ || sub { Params::SomeUtil::_INSTANCE($_[0], $class) }
Subroutine __ANON__: called CC=1 SCAR=0.0
157 31 ▶ } @$must_be;
Condition66%2/3
$test_for{$class} || sub { &Params::SomeUtil::_INSTANCE($_[0], $class); }
l!l&&r!l&&!r
2380
MC/DC0%0/2
$test_for{$class} || sub { &Params::SomeUtil::_INSTANCE($_[0], $class); }
$test_for{$class} sub { &Params::SomeUtil::_INSTANCE($_[0], $class); }
Truth table66%2/3
A || B
A= $test_for{$class}B= sub { &Params::SomeUtil::_INSTANCE($_[0], $class); }
ABresult
1X1
011
000
158  
159 $is_a = (@checks == 1)
160 ? $checks[0]
161 6 ▶ : sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
162 6 my $value = $_[0];
163 12 ▶ List::Util::first { defined($_->($value)) } @checks
Subroutine __ANON__: called CC=1 SCAR=0.0
164 20 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
@checks == 1 ? : 14 6 20
165  
166 20 ▶ $moniker = 'unnamed' unless defined $moniker;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $moniker 0 20 20
167 }
168 }
169  
170 37 ▶ return [] unless $opt_list;
Branch: 1 branch
BranchTrueFalseTotal
unless $opt_list 1 36 37
171  
172 36 ▶ $name_test ||= sub { ! ref $_[0] };
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$name_test ||= sub { not ref $_[0]; }
l!l&&r!l&&!r
1350
MC/DC0%0/2
$name_test ||= sub { not ref $_[0]; }
$name_test sub { not ref $_[0]; }
Truth table66%2/3
A ||= B
A= $name_testB= sub { not ref $_[0]; }
ABresult
1X1
011
000
173  
174 $opt_list = [
175 36 ▶ map { $_ => (ref $opt_list->{$_} ? $opt_list->{$_} : ()) } keys %$opt_list
Branch: 2 branches
BranchTrueFalseTotal
if ref $opt_list eq "HASH" 6 30 36
ref $opt_list->{$_} ? : 5 10 15
176 ] if ref $opt_list eq 'HASH';
177  
178 36 my @return;
179 my %seen;
180  
181 36 ▶ for (my $i = 0; $i < @$opt_list; $i++) { ## no critic
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$opt_list) 86 26 112
182 86 my $name = $opt_list->[$i];
183  
184 86 ▶ if ($require_unique) {
Branch: 1 branch
BranchTrueFalseTotal
if ($require_unique) 28 58 86
185 28 ▶ Carp::croak "multiple definitions provided for $name" if $seen{$name}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$name}++ 1 27 28
186 }
187  
188 85 my $value;
189  
190 85 ▶ if ($i < $#$opt_list) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $opt_list->$#*) 64 21 85
191 64 ▶ if (not defined $opt_list->[$i+1]) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $$opt_list[$i + 1]) { } 6 58 64
elsif (not &$name_test($$opt_list[$i + 1])) { } 29 29 58
192 6 $i++
193 } elsif (! $name_test->($opt_list->[$i+1])) {
194 29 $value = $opt_list->[++$i];
195 29 ▶ if ($is_a && !$is_a->($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_a and not &$is_a($value)) 9 20 29
Condition100%3/3
$is_a and not &$is_a($value)
!ll&&!rl&&r
10109
MC/DC100%2/2
$is_a and not &$is_a($value)
$is_a not &$is_a($value)
Truth table100%3/3
A and not(B)
A= $is_aB= not &$is_a($value)
ABresult
0X0
100
111
196 9 my $ref = ref $value;
197 9 Carp::croak "$ref-ref values are not valid in $moniker opt list";
198 }
199 }
200 }
201  
202 76 push @return, [ $name => $value ];
203 }
204  
205 26 return \@return;
206 }
207  
208 #pod =func mkopt_hash
209 #pod
210 #pod my $opt_hash = Data::OptList::mkopt_hash($input, $moniker, $must_be);
211 #pod
212 #pod Given valid C<L</mkopt>> input, this routine returns a reference to a hash. It
213 #pod will throw an exception if any name has more than one value.
214 #pod
215 #pod =cut
216  
217 12 ▶sub mkopt_hash {
Subroutine mkopt_hash: called CC=2 SCAR=6.9
Pod documented
218 12 my ($opt_list, $moniker, $must_be) = @_;
219 12 ▶ return {} unless $opt_list;
Branch: 1 branch
BranchTrueFalseTotal
unless $opt_list 2 10 12
220  
221 10 $opt_list = mkopt($opt_list, $moniker, 1, $must_be);
222 8 my %hash = map { $_->[0] => $_->[1] } @$opt_list;
223 8 return \%hash;
224 }
225  
226 #pod =head1 EXPORTS
227 #pod
228 #pod Both C<mkopt> and C<mkopt_hash> may be exported on request.
229 #pod
230 #pod =cut
231  
232 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
233 2 *import = Sub::Install::exporter {
234 exports => [qw(mkopt mkopt_hash)],
235 };
236 }
237  
238 2 1;
239  
240 __END__
↑ Summary