Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Passing data to Getopt::Long

by neilwatson (Priest)
on Jul 28, 2015 at 23:56 UTC ( [id://1136681]=perlquestion: print w/replies, xml ) Need Help??

neilwatson has asked for the wisdom of the Perl Monks concerning the following question:

Greetings, Is the following possible? I'm making a template for future scripts and programs. I thought it would be useful to separate the standard cli args (man, help, usage, etc) to a separate package. My first try returns an error. Why?

#!/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; Returns: $ ./foo.pl -du Undefined argument in option spec Error in option spec: "HASH(0x1772fc8)"

Neil Watson
watson-wilson.ca

Replies are listed 'Best First'.
Re: Passing data to getopts::long
by 1nickt (Canon) on Jul 29, 2015 at 00:39 UTC

    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.
Re: Passing data to Getopt::Long
by Laurent_R (Canon) on Jul 29, 2015 at 08:38 UTC
    Hi Neil,

    yes, you could do something like that, but why not call Getopt::Long directly? I do not see much added value in having an intermediate layer here.

Re: Passing data to getopts::long
by Anonymous Monk on Jul 29, 2015 at 00:13 UTC

    Greetings, Is the following possible?

    Yeah but why? Why have your OOPY module return stuff to pass to GetOpt? It should be calling GetOpt directly

    See Getopt::Long::Descriptive , thats the marriage ... but don't return read-only objects (useless)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1136681]
Approved by 1nickt
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-19 04:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found