1Data::OptList(3)      User Contributed Perl Documentation     Data::OptList(3)
2
3
4

NAME

6       Data::OptList - parse and validate simple name/value option pairs
7

VERSION

9       version 0.110
10

SYNOPSIS

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

DESCRIPTION

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

FUNCTIONS

72   mkopt
73         my $opt_list = Data::OptList::mkopt($input, \%arg);
74
75       Valid arguments are:
76
77         moniker        - a word used in errors to describe the opt list; encouraged
78         require_unique - if true, no name may appear more than once
79         must_be        - types to which opt list values are limited (described below)
80         name_test      - a coderef used to test whether a value can be a name
81                          (described below, but you probably don't want this)
82
83       This produces an array of arrays; the inner arrays are name/value
84       pairs.  Values will be either "undef" or a reference.
85
86       Positional parameters may be used for compatibility with the old
87       "mkopt" interface:
88
89         my $opt_list = Data::OptList::mkopt($input, $moniker, $req_uni, $must_be);
90
91       Valid values for $input:
92
93        undef    -> []
94        hashref  -> [ [ key1 => value1 ] ... ] # non-ref values become undef
95        arrayref -> every name followed by a non-name becomes a pair: [ name => ref ]
96                    every name followed by undef becomes a pair: [ name => undef ]
97                    otherwise, it becomes [ name => undef ] like so:
98                    [ "a", "b", [ 1, 2 ] ] -> [ [ a => undef ], [ b => [ 1, 2 ] ] ]
99
100       By default, a name is any defined non-reference.  The "name_test"
101       parameter can be a code ref that tests whether the argument passed it
102       is a name or not.  This should be used rarely.  Interactions between
103       "require_unique" and "name_test" are not yet particularly elegant, as
104       "require_unique" just tests string equality.  This may change.
105
106       The "must_be" parameter is either a scalar or array of scalars; it
107       defines what kind(s) of refs may be values.  If an invalid value is
108       found, an exception is thrown.  If no value is passed for this
109       argument, any reference is valid.  If "must_be" specifies that values
110       must be CODE, HASH, ARRAY, or SCALAR, then Params::Util is used to
111       check whether the given value can provide that interface.  Otherwise,
112       it checks that the given value is an object of the kind.
113
114       In other words:
115
116         [ qw(SCALAR HASH Object::Known) ]
117
118       Means:
119
120         _SCALAR0($value) or _HASH($value) or _INSTANCE($value, 'Object::Known')
121
122   mkopt_hash
123         my $opt_hash = Data::OptList::mkopt_hash($input, $moniker, $must_be);
124
125       Given valid "mkopt" input, this routine returns a reference to a hash.
126       It will throw an exception if any name has more than one value.
127

EXPORTS

129       Both "mkopt" and "mkopt_hash" may be exported on request.
130

AUTHOR

132       Ricardo Signes <rjbs@cpan.org>
133

CONTRIBUTORS

135       ·   Olivier Mengué <dolmen@cpan.org>
136
137       ·   Ricardo SIGNES <rjbs@codesimply.com>
138
140       This software is copyright (c) 2006 by Ricardo Signes.
141
142       This is free software; you can redistribute it and/or modify it under
143       the same terms as the Perl 5 programming language system itself.
144
145
146
147perl v5.28.1                      2016-03-25                  Data::OptList(3)
Impressum