Debian Codegraph @

lib/Data/OptList.pm

raw
1 use strict;
2 use warnings;
3 package Data::OptList 0.115;
4 # ABSTRACT: parse and validate simple name/value option pairs
5
6 use List::Util ();
7 use Params::SomeUtil ();
8 use Sub::Install 0.921 ();
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 my %test_for;
125 BEGIN {
126 %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 sub mkopt {
135 my ($opt_list) = shift;
136
137 my ($moniker, $require_unique, $must_be); # the old positional args
138 my ($name_test, $is_a);
139
140 if (@_) {
141 if (@_ == 1 and Params::SomeUtil::_HASHLIKE($_[0])) {
142 ($moniker, $require_unique, $must_be, $name_test)
143 = @{$_[0]}{ qw(moniker require_unique must_be name_test) };
144 } else {
145 ($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 if (defined $must_be) {
152 $must_be = [ $must_be ] unless ref $must_be;
153 my @checks = map {
154 my $class = $_;
155 $test_for{$class}
156 || sub { Params::SomeUtil::_INSTANCE($_[0], $class) }
157 } @$must_be;
158
159 $is_a = (@checks == 1)
160 ? $checks[0]
161 : sub {
162 my $value = $_[0];
163 List::Util::first { defined($_->($value)) } @checks
164 };
165
166 $moniker = 'unnamed' unless defined $moniker;
167 }
168 }
169
170 return [] unless $opt_list;
171
172 $name_test ||= sub { ! ref $_[0] };
173
174 $opt_list = [
175 map { $_ => (ref $opt_list->{$_} ? $opt_list->{$_} : ()) } keys %$opt_list
176 ] if ref $opt_list eq 'HASH';
177
178 my @return;
179 my %seen;
180
181 for (my $i = 0; $i < @$opt_list; $i++) { ## no critic
182 my $name = $opt_list->[$i];
183
184 if ($require_unique) {
185 Carp::croak "multiple definitions provided for $name" if $seen{$name}++;
186 }
187
188 my $value;
189
190 if ($i < $#$opt_list) {
191 if (not defined $opt_list->[$i+1]) {
192 $i++
193 } elsif (! $name_test->($opt_list->[$i+1])) {
194 $value = $opt_list->[++$i];
195 if ($is_a && !$is_a->($value)) {
196 my $ref = ref $value;
197 Carp::croak "$ref-ref values are not valid in $moniker opt list";
198 }
199 }
200 }
201
202 push @return, [ $name => $value ];
203 }
204
205 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 sub mkopt_hash {
218 my ($opt_list, $moniker, $must_be) = @_;
219 return {} unless $opt_list;
220
221 $opt_list = mkopt($opt_list, $moniker, 1, $must_be);
222 my %hash = map { $_->[0] => $_->[1] } @$opt_list;
223 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 BEGIN {
233 *import = Sub::Install::exporter {
234 exports => [qw(mkopt mkopt_hash)],
235 };
236 }
237
238 1;
239
240 __END__
241
242 =pod
243
244 =encoding UTF-8
245
246 =head1 NAME
247
248 Data::OptList - parse and validate simple name/value option pairs
249
250 =head1 VERSION
251
252 version 0.115
253
254 =head1 SYNOPSIS
255
256 use Data::OptList;
257
258 my $options = Data::OptList::mkopt([
259 qw(key1 key2 key3 key4),
260 key5 => { ... },
261 key6 => [ ... ],
262 key7 => sub { ... },
263 key8 => { ... },
264 key8 => [ ... ],
265 ]);
266
267 ...is the same thing, more or less, as:
268
269 my $options = [
270 [ key1 => undef, ],
271 [ key2 => undef, ],
272 [ key3 => undef, ],
273 [ key4 => undef, ],
274 [ key5 => { ... }, ],
275 [ key6 => [ ... ], ],
276 [ key7 => sub { ... }, ],
277 [ key8 => { ... }, ],
278 [ key8 => [ ... ], ],
279 ]);
280
281 =head1 DESCRIPTION
282
283 Hashes are great for storing named data, but if you want more than one entry
284 for a name, you have to use a list of pairs. Even then, this is really boring
285 to write:
286
287 $values = [
288 foo => undef,
289 bar => undef,
290 baz => undef,
291 xyz => { ... },
292 ];
293
294 Just look at all those undefs! Don't worry, we can get rid of those:
295
296 $values = [
297 map { $_ => undef } qw(foo bar baz),
298 xyz => { ... },
299 ];
300
301 Aaaauuugh! We've saved a little typing, but now it requires thought to read,
302 and thinking is even worse than typing... and it's got a bug! It looked right,
303 didn't it? Well, the C<< xyz => { ... } >> gets consumed by the map, and we
304 don't get the data we wanted.
305
306 With Data::OptList, you can do this instead:
307
308 $values = Data::OptList::mkopt([
309 qw(foo bar baz),
310 xyz => { ... },
311 ]);
312
313 This works by assuming that any defined scalar is a name and any reference
314 following a name is its value.
315
316 =head1 PERL VERSION
317
318 This library should run on perls released even a long time ago. It should
319 work on any version of perl released in the last five years.
320
321 Although it may work on older versions of perl, no guarantee is made that the
322 minimum required version will not be increased. The version may be increased
323 for any reason, and there is no promise that patches will be accepted to
324 lower the minimum required perl.
325
326 =head1 FUNCTIONS
327
328 =head2 mkopt
329
330 my $opt_list = Data::OptList::mkopt($input, \%arg);
331
332 Valid arguments are:
333
334 moniker - a word used in errors to describe the opt list; encouraged
335 require_unique - if true, no name may appear more than once
336 must_be - types to which opt list values are limited (described below)
337 name_test - a coderef used to test whether a value can be a name
338 (described below, but you probably don't want this)
339
340 This produces an array of arrays; the inner arrays are name/value pairs.
341 Values will be either "undef" or a reference.
342
343 Positional parameters may be used for compatibility with the old C<mkopt>
344 interface:
345
346 my $opt_list = Data::OptList::mkopt($input, $moniker, $req_uni, $must_be);
347
348 Valid values for C<$input>:
349
350 undef -> []
351 hashref -> [ [ key1 => value1 ] ... ] # non-ref values become undef
352 arrayref -> every name followed by a non-name becomes a pair: [ name => ref ]
353 every name followed by undef becomes a pair: [ name => undef ]
354 otherwise, it becomes [ name => undef ] like so:
355 [ "a", "b", [ 1, 2 ] ] -> [ [ a => undef ], [ b => [ 1, 2 ] ] ]
356
357 By default, a I<name> is any defined non-reference. The C<name_test> parameter
358 can be a code ref that tests whether the argument passed it is a name or not.
359 This should be used rarely. Interactions between C<require_unique> and
360 C<name_test> are not yet particularly elegant, as C<require_unique> just tests
361 string equality. B<This may change.>
362
363 The C<must_be> parameter is either a scalar or array of scalars; it defines
364 what kind(s) of refs may be values. If an invalid value is found, an exception
365 is thrown. If no value is passed for this argument, any reference is valid.
366 If C<must_be> specifies that values must be CODE, HASH, ARRAY, or SCALAR, then
367 Params::SomeUtil is used to check whether the given value can provide that
368 interface. Otherwise, it checks that the given value is an object of the kind.
369
370 In other words:
371
372 [ qw(SCALAR HASH Object::Known) ]
373
374 Means:
375
376 _SCALAR0($value) or _HASH($value) or _INSTANCE($value, 'Object::Known')
377
378 =head2 mkopt_hash
379
380 my $opt_hash = Data::OptList::mkopt_hash($input, $moniker, $must_be);
381
382 Given valid C<L</mkopt>> input, this routine returns a reference to a hash. It
383 will throw an exception if any name has more than one value.
384
385 =head1 EXPORTS
386
387 Both C<mkopt> and C<mkopt_hash> may be exported on request.
388
389 =head1 AUTHOR
390
391 Ricardo Signes <[email protected]>
392
393 =head1 CONTRIBUTORS
394
395 =for stopwords Olivier Mengué Ricardo Signes
396
397 =over 4
398
399 =item *
400
401 Olivier MenguĂ© <[email protected]>
402
403 =item *
404
405 Ricardo Signes <[email protected]>
406
407 =back
408
409 =head1 COPYRIGHT AND LICENSE
410
411 This software is copyright (c) 2006 by Ricardo Signes.
412
413 This is free software; you can redistribute it and/or modify it under
414 the same terms as the Perl 5 programming language system itself.
415
416 =cut