Actions Status MetaCPAN Release

NAME

Getopt::EX::Hashed - Hash object automation for Getopt::Long

VERSION

Version 1.0602

SYNOPSIS

# script/foo
use App::foo;
App::foo->new->run();

# lib/App/foo.pm
package App::foo;

use Getopt::EX::Hashed; {
    Getopt::EX::Hashed->configure( DEFAULT => [ is => 'rw' ] );
    has start    => ' =i  s begin ' , default => 1;
    has end      => ' =i  e       ' ;
    has file     => ' =s@ f       ' , any => qr/^(?!\.)/;
    has score    => ' =i          ' , min => 0, max => 100;
    has answer   => ' =i          ' , must => sub { $_[1] == 42 };
    has mouse    => ' =s          ' , any => [ 'Frankie', 'Benjy' ];
    has question => ' =s          ' , any => qr/^(life|universe|everything)$/i;
} no Getopt::EX::Hashed;

sub run {
    my $app = shift;
    use Getopt::Long;
    $app->getopt or pod2usage();
    if ($app->answer == 42) {
        $app->question //= 'life';
        ...

DESCRIPTION

Getopt::EX::Hashed is a module to automate the creation of a hash object to store command line option values for Getopt::Long and compatible modules including Getopt::EX::Long. The module name shares the Getopt::EX prefix, but it works independently from other modules in Getopt::EX, so far.

The major objective of this module is integrating initialization and specification into a single place. It also provides a simple validation interface.

Accessor methods are automatically generated when is parameter is given. If the same function is already defined, the program causes fatal error. Accessors are removed when the object is destroyed. Problems may occur when multiple objects are present at the same time.

FUNCTION

has

Declare option parameters in the following form. The parentheses are for clarity only and may be omitted.

has option_name => ( param => value, ... );

For example, to define the option --number, which takes an integer value as a parameter, and also can be used as -n, do the following

has number => spec => "=i n";

The accessor is created with the first name. In this example, the accessor will be defined as $app->number.

If an array reference is given, multiple names can be declared at once.

has [ 'left', 'right' ] => ( spec => "=i" );

If the name starts with plus (+), the given parameter updates the existing setting.

has '+left' => ( default => 1 );

As for the spec parameter, the label can be omitted if it is the first parameter.

has left => "=i", default => 1;

If the number of parameters is odd, the first parameter is treated as having an implicit label: action if it is a code reference, spec otherwise.

Following parameters are available.

Following parameters are all for data validation. First, must is a generic validator and can implement anything. Others are shortcuts for common rules.

METHOD

new

A class method that creates a new hash object. Initializes all members with their default values and creates accessor methods as configured. Returns a blessed hash reference. The hash keys are locked if LOCK_KEYS is enabled.

optspec

Returns the option specification list which can be passed to the GetOptions function.

GetOptions($obj->optspec)

GetOptions has the capability of storing values in a hash by giving the hash reference as the first argument, but it is not necessary.

getopt [ arrayref ]

Calls the appropriate function defined in the caller's context to process options.

$obj->getopt

$obj->getopt(\@argv);

The above examples are shortcuts for the following code.

GetOptions($obj->optspec)

GetOptionsFromArray(\@argv, $obj->optspec)

use_keys keys

When LOCK_KEYS is enabled, accessing a non-existent member causes an error. Use this method to declare new member keys before accessing them.

$obj->use_keys( qw(foo bar) );

If you want to access arbitrary keys, unlock the object.

use Hash::Util 'unlock_keys';
unlock_keys %{$obj};

You can change this behavior by configure with LOCK_KEYS parameter.

configure label => value, ...

Use class method Getopt::EX::Hashed->configure() before creating an object; this information is stored separately for each calling package. After calling new(), the package-level configuration is copied into the object for its use. Use $obj->configure() to update object-level configuration.

The following configuration parameters are available.

reset

Reset the class to the original state.

SEE ALSO

Getopt::Long

Getopt::EX, Getopt::EX::Long

AUTHOR

Kazumasa Utashiro

COPYRIGHT

The following copyright notice applies to all the files provided in this distribution, including binary files, unless explicitly noted otherwise.

Copyright 2021-2025 Kazumasa Utashiro

LICENSE

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