NAME

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

VERSION

version 0.01

$Id$

SYNOPSIS

use Data::OptList;

my $options = Data::Optlist::canonicalize_opt_list([
  qw(key1 key2 key3 key4),
  key5 => { ... },
  key6 => [ ... ],
  key7 => sub { ... },
  key8 => { ... },
  key8 => [ ... ],
]);

...is the same thing, more or less, as:

my $options = [
  [ key1 => undef,        ],
  [ key2 => undef,        ],
  [ key3 => undef,        ],
  [ key4 => undef,        ],
  [ key5 => { ... },      ],
  [ key6 => [ ... ],      ],
  [ key7 => sub { ... },  ],
  [ key8 => { ... },      ],
  [ key8 => [ ... ],      ],
]);

DESCRIPTION

Hashes are great for storing named data, but if you want more than one entry for a name, you have to use a list of pairs. Even then, this is really boring to write:

@values = (
  foo => undef,
  bar => undef,
  baz => undef,
  xyz => { ... },
);

Just look at all those undefs! Don't worry, we can get rid of those:

@values = (
  map { $_ => undef } qw(foo bar baz),
  xyz => { ... },
);

Aaaauuugh! We've saved a little typing, but now it requires thought to read, and thinking is even worse than typing.

With Data::OptList, you can do this instead:

Data::OptList::canonicalize_opt_list([
  qw(foo bar baz),
  xyz => { ... },
]);

This works by assuming that any defined scalar is a name and any reference following a name is its value.

FUNCTIONS

canonicalize_opt_list

Warning: This modules presently exists only to serve Sub::Exporter. Its interface is still subject to change at the author's whim.

my $opt_list = Data::OptList::canonicalize_opt_list(
  $input,
  $moniker,
  $require_unique,
  $must_be,
);

This produces an array of arrays; the inner arrays are name/value pairs. Values will be either "undef" or a reference.

Valid inputs:

undef    -> []
hashref  -> [ [ key1 => value1 ] ... ] # non-ref values become undef
arrayref -> every value followed by a ref becomes a pair: [ value => ref   ]
            every value followed by undef becomes a pair: [ value => undef ]
            otherwise, it becomes [ value => undef ] like so:
            [ "a", "b", [ 1, 2 ] ] -> [ [ a => undef ], [ b => [ 1, 2 ] ] ]

$moniker is a name describing the data, which will be used in error messages.

If $require_unique is true, an error will be thrown if any name is given more than once.

$must_be is either a scalar or array of scalars; it defines what kind(s) of refs may be values. If an invalid value is found, an exception is thrown. If no value is passed for this argument, any reference is valid.

expand_opt_list

my $opt_hash = Data::OptList::expand_opt_list($input, $moniker, $must_be);

Given valid expand_opt_list input, return a hash.

TODO

I'd really like to decide I'm happy with the interface so I can take out those "Warning" messages that everyone will probably ignore anyway. Then I'll make this its own dist.

AUTHOR

Ricardo SIGNES, <rjbs@cpan.org>

BUGS

Please report any bugs or feature requests to bug-sub-exporter@rt.cpan.org, or through the web interface at http://rt.cpan.org. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

COPYRIGHT

Copyright 2006 Ricardo SIGNES. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.