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

Hi all,
I wrote a code that can be used to parse the input arguments using Getopt module.
Please go through the code and if I am wrong please send me the suggestion to write the better code.
# Get command line options %options = program_options() or die "Error!"; sub program_options { my ( %options, @informations, @errors, ); use Pod::Usage; # Get command line options use Getopt::Long; GetOptions('help!' => \$options{help}, 'verbose' => \$options{verbose}, 'name:s' => \$options{name}, ) or die "Couldn't read command line options"; push @informations, "Usage:\n To execute: perl ProgramName <argument>\ +n Help: perl ProgramName --help or perl ProgramName --h" if $options{ +help); pod2usage (join "\n", @informations) if @informations; # Exit with usage if any needed option is missing push @errors, "Input is required" unless $options{name}; pod2usage (join "\n", @errors) if @errors; }

Thanks, Sur

Retitled by holli from 'Can anyone please'.

Replies are listed 'Best First'.
Re: Need help with Getopt::Long and Pod::Usage
by dragonchild (Archbishop) on May 31, 2005 at 13:54 UTC
    You don't tell us what's going wrong, but I'm going to guess that changing the bottom of your subroutine to look like:
    pod2usage (join "\n", @errors) if @errors; return %options; # <-- Add this line }
    will fix it.

    • In general, if you think something isn't in Perl, try it out, because it usually is. :-)
    • "What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?"
Re: Need help with Getopt::Long and Pod::Usage
by newest_newbie (Acolyte) on May 31, 2005 at 14:51 UTC
    Hi,
    Though I am not sure what your problem is, one mistake I found in your code is in the line push @informations ... statement.
    Should be
    push @informations, "Usage:\n To execute: perl ProgramName <argument>\ +n Help: perl ProgramName --help or perl ProgramName --h" if $options{ +help};

    instead of
    push @informations, "Usage:\n To execute: perl ProgramName <argument>\ +n Help: perl ProgramName --help or perl ProgramName --h" if $options{ +help);

    Hope that helps
Re: Need help with Getopt::Long and Pod::Usage
by herveus (Prior) on Jun 01, 2005 at 12:08 UTC
A reply falls below the community's threshold of quality. You may see it by logging in.