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.101
10
11 $Id: /my/cs/projects/optlist/trunk/lib/Data/OptList.pm 22474 2006-06-13T12:44:51.413983Z rjbs $
12
14 use Data::OptList;
15
16 my $options = Data::Optlist::mkopt([
17 qw(key1 key2 key3 key4),
18 key5 => { ... },
19 key6 => [ ... ],
20 key7 => sub { ... },
21 key8 => { ... },
22 key8 => [ ... ],
23 ]);
24
25 ...is the same thing, more or less, as:
26
27 my $options = [
28 [ key1 => undef, ],
29 [ key2 => undef, ],
30 [ key3 => undef, ],
31 [ key4 => undef, ],
32 [ key5 => { ... }, ],
33 [ key6 => [ ... ], ],
34 [ key7 => sub { ... }, ],
35 [ key8 => { ... }, ],
36 [ key8 => [ ... ], ],
37 ]);
38
40 Hashes are great for storing named data, but if you want more than one
41 entry for a name, you have to use a list of pairs. Even then, this is
42 really boring to write:
43
44 @values = (
45 foo => undef,
46 bar => undef,
47 baz => undef,
48 xyz => { ... },
49 );
50
51 Just look at all those undefs! Don't worry, we can get rid of those:
52
53 @values = (
54 map { $_ => undef } qw(foo bar baz),
55 xyz => { ... },
56 );
57
58 Aaaauuugh! We've saved a little typing, but now it requires thought to
59 read, and thinking is even worse than typing.
60
61 With Data::OptList, you can do this instead:
62
63 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 refer‐
69 ence following a name is its value.
70
72 mkopt
73
74 my $opt_list = Data::OptList::mkopt(
75 $input,
76 $moniker,
77 $require_unique,
78 $must_be,
79 );
80
81 This produces an array of arrays; the inner arrays are name/value
82 pairs. Values will be either "undef" or a reference.
83
84 Valid inputs:
85
86 undef -> []
87 hashref -> [ [ key1 => value1 ] ... ] # non-ref values become undef
88 arrayref -> every value followed by a ref becomes a pair: [ value => ref ]
89 every value followed by undef becomes a pair: [ value => undef ]
90 otherwise, it becomes [ value => undef ] like so:
91 [ "a", "b", [ 1, 2 ] ] -> [ [ a => undef ], [ b => [ 1, 2 ] ] ]
92
93 $moniker is a name describing the data, which will be used in error
94 messages.
95
96 If $require_unique is true, an error will be thrown if any name is
97 given more than once.
98
99 $must_be is either a scalar or array of scalars; it defines what
100 kind(s) of refs may be values. If an invalid value is found, an excep‐
101 tion is thrown. If no value is passed for this argument, any reference
102 is valid.
103
104 mkopt_hash
105
106 my $opt_hash = Data::OptList::mkopt_hash($input, $moniker, $must_be);
107
108 Given valid "mkopt" input, this routine returns a hash. It will throw
109 an exception if any name has more than one value.
110
112 Both "mkopt" and "mkopt_hash" may be exported on request.
113
115 Ricardo SIGNES, "<rjbs@cpan.org>"
116
118 Please report any bugs or feature requests to
119 "bug-sub-exporter@rt.cpan.org", or through the web interface at
120 <http://rt.cpan.org>. I will be notified, and then you'll automatically
121 be notified of progress on your bug as I make changes.
122
124 Copyright 2006 Ricardo SIGNES. This program is free software; you can
125 redistribute it and/or modify it under the same terms as Perl itself.
126
127
128
129perl v5.8.8 2006-06-13 Data::OptList(3)