8 use Sub::Install 0.92 ();
12 Data::OptList - parse and validate simple name/value option pairs
20 our $VERSION = '0.104';
26 my $options = Data::Optlist::mkopt([
27 qw(key1 key2 key3 key4),
35 ...is the same thing, more or less, as:
44 [ key7 => sub { ... }, ],
51 Hashes are great for storing named data, but if you want more than one entry
52 for a name, you have to use a list of pairs. Even then, this is really boring
62 Just look at all those undefs! Don't worry, we can get rid of those:
65 map { $_ => undef } qw(foo bar baz),
69 Aaaauuugh! We've saved a little typing, but now it requires thought to read,
70 and thinking is even worse than typing... and it's got a bug! It looked right,
71 didn't it? Well, the C<< xyz => { ... } >> gets consumed by the map, and we
72 don't get the data we wanted.
74 With Data::OptList, you can do this instead:
76 $values = Data::OptList::mkopt([
81 This works by assuming that any defined scalar is a name and any reference
82 following a name is its value.
88 my $opt_list = Data::OptList::mkopt(
95 This produces an array of arrays; the inner arrays are name/value pairs.
96 Values will be either "undef" or a reference.
98 Valid values for C<$input>:
101 hashref -> [ [ key1 => value1 ] ... ] # non-ref values become undef
102 arrayref -> every value followed by a ref becomes a pair: [ value => ref ]
103 every value followed by undef becomes a pair: [ value => undef ]
104 otherwise, it becomes [ value => undef ] like so:
105 [ "a", "b", [ 1, 2 ] ] -> [ [ a => undef ], [ b => [ 1, 2 ] ] ]
107 C<$moniker> is a name describing the data, which will be used in error
110 If C<$require_unique> is true, an error will be thrown if any name is given
113 C<$must_be> is either a scalar or array of scalars; it defines what kind(s) of
114 refs may be values. If an invalid value is found, an exception is thrown. If
115 no value is passed for this argument, any reference is valid. If C<$must_be>
116 specifies that values must be CODE, HASH, ARRAY, or SCALAR, then Params::Util
117 is used to check whether the given value can provide that interface.
118 Otherwise, it checks that the given value is an object of the kind.
122 [ qw(SCALAR HASH Object::Known) ]
126 _SCALAR0($value) or _HASH($value) or _INSTANCE($value, 'Object::Known')
133 CODE => \&Params::Util::_CODELIKE, ## no critic
134 HASH => \&Params::Util::_HASHLIKE, ## no critic
135 ARRAY => \&Params::Util::_ARRAYLIKE, ## no critic
136 SCALAR => \&Params::Util::_SCALAR0, ## no critic
141 my ($got, $expected) = @_;
143 return List::Util::first { __is_a($got, $_) } @$expected if ref $expected;
146 exists($test_for{$expected})
147 ? $test_for{$expected}->($got)
148 : Params::Util::_INSTANCE($got, $expected) ## no critic
153 my ($opt_list, $moniker, $require_unique, $must_be) = @_;
155 return [] unless $opt_list;
158 map { $_ => (ref $opt_list->{$_} ? $opt_list->{$_} : ()) } keys %$opt_list
159 ] if ref $opt_list eq 'HASH';
164 for (my $i = 0; $i < @$opt_list; $i++) { ## no critic
165 my $name = $opt_list->[$i];
168 if ($require_unique) {
169 Carp::croak "multiple definitions provided for $name" if $seen{$name}++;
172 if ($i == $#$opt_list) { $value = undef; }
173 elsif (not defined $opt_list->[$i+1]) { $value = undef; $i++ }
174 elsif (ref $opt_list->[$i+1]) { $value = $opt_list->[++$i] }
175 else { $value = undef; }
177 if ($must_be and defined $value) {
178 unless (__is_a($value, $must_be)) {
179 my $ref = ref $value;
180 Carp::croak "$ref-ref values are not valid in $moniker opt list";
184 push @return, [ $name => $value ];
192 my $opt_hash = Data::OptList::mkopt_hash($input, $moniker, $must_be);
194 Given valid C<L</mkopt>> input, this routine returns a reference to a hash. It
195 will throw an exception if any name has more than one value.
200 my ($opt_list, $moniker, $must_be) = @_;
201 return {} unless $opt_list;
203 $opt_list = mkopt($opt_list, $moniker, 1, $must_be);
204 my %hash = map { $_->[0] => $_->[1] } @$opt_list;
210 Both C<mkopt> and C<mkopt_hash> may be exported on request.
215 *import = Sub::Install::exporter {
216 exports => [qw(mkopt mkopt_hash)],
222 Ricardo SIGNES, C<< <rjbs@cpan.org> >>
226 Please report any bugs or feature requests at L<http://rt.cpan.org>. I will be
227 notified, and then you'll automatically be notified of progress on your bug as
232 Copyright 2006-2007, Ricardo SIGNES. This program is free software; you can
233 redistribute it and/or modify it under the same terms as Perl itself.