Data::OptList(3)

🌸 Data::OptList 🌸

Okay so bestie, this is Data::OptList(3) from the User Contributed Perl Documentation, and it is literally here to parse and validate simple name/value option pairs. Periodt.

πŸ’… NAME πŸ’…

Data::OptList β€” parse and validate simple name/value option pairs. That's the whole assignment, and it ate. πŸ’–

✨ VERSION ✨

version 0.114 β€” yes queen, that's the version.

πŸ¦‹ SYNOPSIS πŸ¦‹

  use Data::OptList;

  my $options = Data::OptList::mkopt([
    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 iconic for storing named data, but the second you want more than one entry for a name, you have to use a list of pairs. And even then, writing it by hand is lowkey so boring:

  $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 saved a little typing, but now it requires thought to read, and thinking is even worse than typing... and it's got a bug! It looked right, didn't it? Well, the "xyz => { ... }" gets consumed by the map, and we don't get the data we wanted.

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

  $values = Data::OptList::mkopt([
    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. Vibe check passed. πŸ’…

πŸͺ PERL VERSION πŸͺ

This library should run on perls released even a long time ago. It should work on any version of perl released in the last five years.

Although it may work on older versions of perl, no guarantee is made that the minimum required version will not be increased. The version may be increased for any reason, and there is no promise that patches will be accepted to lower the minimum required perl.

πŸ› οΈ FUNCTIONS πŸ› οΈ

Here's where the actual functions live, bestie.

πŸ’– mkopt πŸ’–

  my $opt_list = Data::OptList::mkopt($input, \%arg);

Valid arguments are:

  • moniker β€” a word used in errors to describe the opt list; encouraged
  • require_unique β€” if true, no name may appear more than once
  • must_be β€” types to which opt list values are limited (described below)
  • name_test β€” a coderef used to test whether a value can be a name (described below, but you probably don't want this)

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

Positional parameters may be used for compatibility with the old mkopt interface:

  my $opt_list = Data::OptList::mkopt($input, $moniker, $req_uni, $must_be);

Valid values for $input:

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

By default, a name is any defined non-reference. The name_test parameter can be a code ref that tests whether the argument passed it is a name or not. This should be used rarely. Interactions between require_unique and name_test are not yet particularly elegant, as require_unique just tests string equality. This may change.

The must_be parameter 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. If must_be specifies that values must be CODE, HASH, ARRAY, or SCALAR, then Params::Util is used to check whether the given value can provide that interface. Otherwise, it checks that the given value is an object of the kind.

In other words:

  [ qw(SCALAR HASH Object::Known) ]

Means:

  _SCALAR0($value) or _HASH($value) or _INSTANCE($value, 'Object::Known')

πŸ’– mkopt_hash πŸ’–

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

Given valid mkopt input, this routine returns a reference to a hash. It will throw an exception if any name has more than one value.

🎁 EXPORTS 🎁

Both mkopt and mkopt_hash may be exported on request.

✍️ AUTHOR ✍️

Ricardo Signes <cpan@semiotic.systems>

🌟 CONTRIBUTORS 🌟

  • Olivier MenguΓ© <dolmen@cpan.org>
  • Ricardo Signes <rjbs@semiotic.systems>

This software is copyright (c) 2006 by Ricardo Signes.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.


And that's the page, bestie! Footer cuteness: this is from perl v5.42.0, dated 2023-05-07 β€” Data::OptList(3) signing off. πŸ’–βœ¨πŸ«Ά

πŸ“¦ where this page comes from