Getopt...Hashed(3pm)
π Getopt::EX::Hashed β the group chat version π
okay bestie so this is Getopt::EX::Hashed, a User Contributed Perl Documentation page, version 1.0702. It's giving "hash object but make it automatic" and honestly? obsessed. Let's get into it π«Ά
πΈ NAME πΈ
Getopt::EX::Hashed β Hash object automation for Getopt::Long β¨
β VERSION β
Version 1.0702 π
π SYNOPSIS π
here's the whole vibe, look:
# 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';
...
like. it's so clean. no cap. π₯
π¦ DESCRIPTION π¦
Getopt::EX::Hashed is a module that automates making a hash object to store your command line option values for Getopt::Long and its compatible besties, including Getopt::EX::Long. The module name shares the Getopt::EX prefix, but it works independently from other modules in Getopt::EX, so far. okay so the family name is shared but she's independent, she pays her own rent π
The major objective here is integrating initialization and specification into a single place. She also provides a simple validation interface, bc we love a module that checks on you πΈ
Accessor methods are automatically generated when the is parameter is given. If the same function is already defined, the program causes fatal error. π By default, accessors are removed from the package namespace when the object is destroyed. This allows the same class to be used more than once within a single script β for example, when a module using Hashed-based objects is called from multiple places. so she cleans up after herself, iconic.
Objects created by copying (e.g. via Clone) are not considered owners of the accessors. Only the object originally created by new removes the accessors on destruction, so cloned objects can be safely destroyed without affecting the original object or its class. To keep accessors after destruction, set REMOVE_ACCESSOR to false. π«Ά
β¨ FUNCTION β¨
π has
Declare option parameters in the following form. The parentheses are for clarity only and may be omitted, she's chill like that.
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. group project energy:
has [ 'left', 'right' ] => ( spec => "=i" );
If the name starts with plus (+), the given parameter updates the existing setting. glow up, not rewrite:
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. she's reading the vibe, not the labels β¨
Following parameters are available. ok here comes the menu, hydrate first π§
- [ spec => ] string β Give option specification.
spec =>label can be omitted if and only if it is the first parameter.
In string, option spec and alias names are separated by white space, and can show up in any order. no rules, just vibes.
To have an option called --start that takes an integer as its value and can also be used with the names -s and --begin, declare as follows.
has start => "=i s begin";
The above declaration will be compiled into the following string.
start|s|begin=i
which conforms to the Getopt::Long definition. Of course, you can write it as:
has start => "s|begin=i";
If the name and aliases contain underscore (_), another alias name is defined with dash (-) in place of underscores. she does the formatting for you, slay.
has a_to_z => "=s";
The above declaration will be compiled into the following string.
a_to_z|a-to-z=s
If no option spec is needed, give an empty (or white space only) string as a value. Without a spec string, the member will not be treated as an option. π
- alias => string β Additional alias names can be specified by the alias parameter too. There is no difference from the ones in the
specparameter. same thing, different font.
has start => "=i", alias => "s begin";
- is => "ro" | "rw" β To produce an accessor method, the
isparameter is necessary. Set the valuerofor read-only,rwfor read-write. pick your fighter π
Read-write accessor has lvalue attribute, so it can be assigned to. You can use like this:
$app->foo //= 1;
This is much simpler than writing as in the following.
$app->foo(1) unless defined $app->foo;
If you want to make accessors for all following members, use configure to set the DEFAULT parameter.
Getopt::EX::Hashed->configure( DEFAULT => [ is => 'rw' ] );
If you don't like assignable accessors, configure the ACCESSOR_LVALUE parameter to 0. Because accessors are generated at the time of new, this value is effective for all members. (so set it before new or it's not listening, bestie)
- default => value | coderef β Set default value. If no default is given, the member is initialized as
undef.
If the value is a reference to an ARRAY or HASH, a shallow copy is created for each new call. The elements are not deep-copied, so if any element is itself a reference, the referenced data is shared across instances. heads up fr fr, shared references are shared references π
If a code reference is given, it is called at the time of new to get default value. This is effective when you want to evaluate the value at the time of execution, rather than declaration. If you want to define a default action, use the action parameter. If you want to set code reference as the initial value, you must specify a code reference that returns a code reference. (yes really, coderef-ception π)
If a reference to SCALAR is given, the option value is stored in the data indicated by the reference, not in the hash object member. In this case, the expected value cannot be obtained by accessing the hash member. not me accidentally reading the wrong thing π
- [ action => ] coderef β Parameter
actiontakes code reference which is called to process the option.action =>label can be omitted if and only if it is the first parameter.
When called, hash object is passed as $_.
has [ qw(left right both) ] => '=i';
has "+both" => sub {
$_->{left} = $_->{right} = $_[1];
};
You can use this for <> to handle non-option arguments. In that case, the spec parameter does not matter and is not required. she's flexible like that π¦
has ARGV => default => [];
has "<>" => sub {
push @{$_->{ARGV}}, $_[0];
};
Following parameters are all for data validation. First, must is a generic validator and can implement anything. Others are shortcuts for common rules. π
- must => coderef | [ coderef ... ] β Parameter
musttakes a code reference to validate option values. It takes the same arguments asactionand returns a boolean. With the following example, option--answertakes only42as a valid value.
has answer => '=i',
must => sub { $_[1] == 42 };
If multiple code references are given, all code must return true. everyone has to agree, it's a group decision.
has answer => '=i',
must => [ sub { $_[1] >= 42 }, sub { $_[1] <= 42 } ];
- min => number
- max => number β Set the minimum and maximum limit for the argument. boundaries, but make it slay β¨
- any => arrayref | qr/regex/ | coderef β Set the valid string parameter list. Each item can be a string, a regex reference, or a code reference. The argument is valid when it is the same as, or matches any item of the given list. If the value is not an arrayref, it is taken as a single item list (regexpref or coderef usually).
Following declarations are almost equivalent, except second one is case insensitive. π
has question => '=s',
any => [ 'life', 'universe', 'everything' ];
has question => '=s',
any => qr/^(life|universe|everything)$/i;
If you are using optional argument, don't forget to include default value in the list. Otherwise it causes validation error. she will absolutely catch you lacking π
has question => ':s',
any => [ 'life', 'universe', 'everything', '' ];
ok that's the first half bestie, and we are SO not done β more parts coming π don't go anywhere π
π METHOD π
Okay bestie, this is where the object actually does its little dance. β¨
β¨ new β¨
A class method that creates a new hash object. It 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. (iykyk πββοΈ)
β¨ 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 the 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. π
LOCK_KEYS(default:1) β Lock hash keys. This prevents typos or other mistakes from creating unintended hash entries. πREPLACE_UNDERSCORE(default:1) β Automatically create option aliases with underscores replaced by dashes. πREMOVE_UNDERSCORE(default:0) β Automatically create option aliases with underscores removed. πGETOPT(default:'GetOptions') β Set the function name called from thegetoptmethod.GETOPT_FROM_ARRAY(default:'GetOptionsFromArray') β Set the function name called from thegetoptmethod.ACCESSOR_PREFIX(default:'') β When specified, it will be prepended to the member name to make the accessor method. IfACCESSOR_PREFIXis defined asopt_, the accessor for memberfilewill beopt_file. πͺACCESSOR_LVALUE(default:1) β If true, read-write accessors have the lvalue attribute. Set to zero if you don't like that behavior.REMOVE_ACCESSOR(default:1) β If true, accessor methods are removed from the package namespace when the object is destroyed. This allows the same class to be used multiple times within a single script without causing conflicts.
Only the object originally created by new is considered the owner of the accessors. Objects created by copying (e.g. via Clone) are not owners, so destroying a clone does not remove the accessors even when REMOVE_ACCESSOR is true.
Set to false if you want accessors to persist beyond the lifetime of the object, for example when the accessor methods are intended to be part of the class's permanent public interface.
DEFAULTβ Set default parameters. Whenhasis called, DEFAULT parameters are inserted before the explicit parameters. If a parameter appears in both, the explicit one takes precedence. Incremental calls with+are not affected.
A typical use of DEFAULT is is to prepare accessor methods for all following hash entries. Declare DEFAULT => [] to reset.
Getopt::EX::Hashed->configure(DEFAULT => [ is => 'ro' ]);
β¨ reset β¨
Reset the class to the original state. π§Ό
β¨ SEE ALSO β¨
Getopt::LongGetopt::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-2026 Kazumasa Utashiro
β¨ LICENSE β¨
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
That's a wrap on Getopt...Hashed(3pm) from perl v5.40.1, dated 2026-03-27 β love u bestie! πβ¨
π¦ where this page comes from
- ubuntu stonking: libgetopt-ex-hashed-perl 1.0702-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms