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.02

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 default settings in a single hash of default values. That is, no more redundant naming and scalar references.

So, instead of these eight statements and seven 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 label once and hold default values in a hash explicitly, instead of in separate scalars, three statements and two variables.

  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;
  $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.