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.112
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

PERL VERSION SUPPORT

72       This module has a long-term perl support period.  That means it will
73       not require a version of perl released fewer than five years ago.
74
75       Although it may work on older versions of perl, no guarantee is made
76       that the minimum required version will not be increased.  The version
77       may be increased for any reason, and there is no promise that patches
78       will be accepted to lower the minimum required perl.
79

FUNCTIONS

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

EXPORTS

138       Both "mkopt" and "mkopt_hash" may be exported on request.
139

AUTHOR

141       Ricardo Signes <rjbs@semiotic.systems>
142

CONTRIBUTOR

144       Olivier Mengué <dolmen@cpan.org>
145
147       This software is copyright (c) 2006 by Ricardo Signes.
148
149       This is free software; you can redistribute it and/or modify it under
150       the same terms as the Perl 5 programming language system itself.
151
152
153
154perl v5.34.0                      2022-01-21                  Data::OptList(3)
Impressum