in reply to Passing data to Getopt::Long

Pass hashes instead of hashrefs to GetOptions.

#!/usr/bin/perl use strict; use warnings; use feature 'say'; use Getopt::Long; use Pod::Usage; use Data::Dumper; my $VERSION = 1; # Get standard options from reusable module my $stdopts = Std::Opts->new(); my $std_cli_arg_ref = $stdopts->get_standard_args; # Read, process, and validate cli args my %cli_arg_ref; GetOptions( %cli_arg_ref, %{ $std_cli_arg_ref }, # These two samples can be removed 'myarg=s', 'arg2=i', ); # # Modules # package Std::Opts; use strict; use warnings; sub new { my ( $class ) = @_; return bless {}, $class; } sub get_standard_args { my $self = shift; my $std_cli_arg_ref = { 'version' => sub { say $VERSION; exit + }, 'test' => sub { _run_tests(); exit + }, 'man' => sub { pod2usage( -verbose => 2, -exitval => 0 ) + }, 'dumpargs' => sub { say '%cli_arg_ref = '. Dumper( \%cli_arg_ref ); exit }, 'help|?' => sub { pod2usage( -sections => ['OPTIONS'], -exitval => 0, -verbose + => 99) }, 'usage' => sub { pod2usage( -sections => ['SYNOPSIS'], -exitval => 0, -verbose + => 99) }, 'examples' => sub { pod2usage( -sections => 'EXAMPLES', -exitval => 0, -verbose + => 99) }, }; return $std_cli_arg_ref; } 1;
[17:39][nick:~/monks]$ perl 1136681.pl --version 1
The way forward always starts with a minimal test.