The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Getopt::HashDefaults - Single declaration default settings

VERSION

version 0.0201

SYNOPSIS

  use Getopt::HashDefaults;
  my $options = { 'length=i' => 123.45, 'help|?' => 0 };
  my @config = qw(debug);
  my $o = Getopt::HashDefaults->new(@config);
  $options = $o->getoptions($options);
  print "Length: $options->{length}\n";

DESCRIPTION

Getopt::HashDefaults allows you to use a single hash of default values to define Getop::Long specs, labels and values, in a single, flat hash.

So, instead of these 8 statements and 7 variables:

  my $verbose = 0;
  my $debug   = 0;
  my $filter  = 1;
  my $length  = 3.1415;
  my $size    = 1_000_000;
  my $colours = [qw(a b c)];
  my %h = ();
  GetOptions(\%h, 'verbose', 'debug', 'filter', 'length=i', 'size=i', 'colours=s@');

Getopt::HashDefaults lets you declare a spec, label and default value once. It lets you hold these default values in a hash explicitly, instead of separate scalars and references. (Also: only 3 statements, 2 variables and less chars)

  my $h = {
    'verbose'    => 0,
    'debug'      => 0,
    'filter'     => 1,
    'length=i'   => 3.1415,
    'size=i'     => 1_000_000,
    'colours=s@' => [qw(a b c)],
  };
  my $o = Getopt::HashDefaults->new;
  $h = $o->getoptions($h);

NAME

Getopt::HashDefaults - Single declaration default settings

METHODS

new()

 $o = Getopt::HashDefaults->new;

Create a new Getopt::HashDefaults instance.

getoptions()

Accept a hashref of Getopt::Long arguments, extract the argument specs and call the object oriented version of "getoptions" in Getopt::Long.

SEE ALSO

Getopt::Long

AUTHOR

Gene Boggs <gene@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2013 by Gene Boggs.

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