1Data::OptList(3) User Contributed Perl Documentation Data::OptList(3)
2
3
4
6 Data::OptList - parse and validate simple name/value option pairs
7
9 version 0.104
10
12 use Data::OptList;
13
14 my $options = Data::Optlist::mkopt([
15 qw(key1 key2 key3 key4),
16 key5 => { ... },
17 key6 => [ ... ],
18 key7 => sub { ... },
19 key8 => { ... },
20 key8 => [ ... ],
21 ]);
22
23 ...is the same thing, more or less, as:
24
25 my $options = [
26 [ key1 => undef, ],
27 [ key2 => undef, ],
28 [ key3 => undef, ],
29 [ key4 => undef, ],
30 [ key5 => { ... }, ],
31 [ key6 => [ ... ], ],
32 [ key7 => sub { ... }, ],
33 [ key8 => { ... }, ],
34 [ key8 => [ ... ], ],
35 ]);
36
38 Hashes are great for storing named data, but if you want more than one
39 entry for a name, you have to use a list of pairs. Even then, this is
40 really boring to write:
41
42 $values = [
43 foo => undef,
44 bar => undef,
45 baz => undef,
46 xyz => { ... },
47 ];
48
49 Just look at all those undefs! Don't worry, we can get rid of those:
50
51 $values = [
52 map { $_ => undef } qw(foo bar baz),
53 xyz => { ... },
54 ];
55
56 Aaaauuugh! We've saved a little typing, but now it requires thought to
57 read, and thinking is even worse than typing... and it's got a bug! It
58 looked right, didn't it? Well, the "xyz => { ... }" gets consumed by
59 the map, and we don't get the data we wanted.
60
61 With Data::OptList, you can do this instead:
62
63 $values = Data::OptList::mkopt([
64 qw(foo bar baz),
65 xyz => { ... },
66 ]);
67
68 This works by assuming that any defined scalar is a name and any
69 reference following a name is its value.
70
72 mkopt
73 my $opt_list = Data::OptList::mkopt(
74 $input,
75 $moniker,
76 $require_unique,
77 $must_be,
78 );
79
80 This produces an array of arrays; the inner arrays are name/value
81 pairs. Values will be either "undef" or a reference.
82
83 Valid values for $input:
84
85 undef -> []
86 hashref -> [ [ key1 => value1 ] ... ] # non-ref values become undef
87 arrayref -> every value followed by a ref becomes a pair: [ value => ref ]
88 every value followed by undef becomes a pair: [ value => undef ]
89 otherwise, it becomes [ value => undef ] like so:
90 [ "a", "b", [ 1, 2 ] ] -> [ [ a => undef ], [ b => [ 1, 2 ] ] ]
91
92 $moniker is a name describing the data, which will be used in error
93 messages.
94
95 If $require_unique is true, an error will be thrown if any name is
96 given more than once.
97
98 $must_be is either a scalar or array of scalars; it defines what
99 kind(s) of refs may be values. If an invalid value is found, an
100 exception is thrown. If no value is passed for this argument, any
101 reference is valid. If $must_be specifies that values must be CODE,
102 HASH, ARRAY, or SCALAR, then Params::Util is used to check whether the
103 given value can provide that interface. Otherwise, it checks that the
104 given value is an object of the kind.
105
106 In other words:
107
108 [ qw(SCALAR HASH Object::Known) ]
109
110 Means:
111
112 _SCALAR0($value) or _HASH($value) or _INSTANCE($value, 'Object::Known')
113
114 mkopt_hash
115 my $opt_hash = Data::OptList::mkopt_hash($input, $moniker, $must_be);
116
117 Given valid "mkopt" input, this routine returns a reference to a hash.
118 It will throw an exception if any name has more than one value.
119
121 Both "mkopt" and "mkopt_hash" may be exported on request.
122
124 Ricardo SIGNES, "<rjbs@cpan.org>"
125
127 Please report any bugs or feature requests at <http://rt.cpan.org>. I
128 will be notified, and then you'll automatically be notified of progress
129 on your bug as I make changes.
130
132 Copyright 2006-2007, Ricardo SIGNES. This program is free software;
133 you can redistribute it and/or modify it under the same terms as Perl
134 itself.
135
136
137
138perl v5.10.1 2009-01-16 Data::OptList(3)