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

My test code is as follows:
use strict; use warnings; use diagnostics; use Getopt::Long; my %Options = ("Verbose" => 0); GetOptions( \%Options , "TPID=s" , "User=s" , "Password=s" , "DataFile=s" , "Table=s" , "DataBase=s" , "Sessions:i" , "Verbose=i" ) or warn("Waaaaaaahhhhhhhhh!!!!!!"); foreach (sort(keys(%Options))) { print("Options[$_][" . $Options{$_} . "]\n"); }
All I get back is:
Options[Verbose][0]
Which is what I initialized %Options with in the first palce. %Options is not getting filled in.

I am pretty new to GetOpt::Long, it seems like it should work, but it doesn't? What am I missing?

Update: I answered my own question, I wasn't putting '-' in front of my parameters on the command line. Once I did that it worked.

Replies are listed 'Best First'.
Re: Why is my GetOptions() Not Working?
by gam3 (Curate) on Mar 31, 2005 at 22:45 UTC
    Yes you need a '-' or '--' before the option.
    $ perl getopt.pl Verbose=1 Options[Verbose][0] $ perl getopt.pl -Verbose=1 Options[Verbose][1] $perl getopt.pl --Verbose=1 Options[Verbose][1]
    where getopt.pl is you code.
    -- gam3
    A picture is worth a thousand words, but takes 200K.
Re: Why is my GetOptions() Not Working?
by moot (Chaplain) on Mar 31, 2005 at 22:00 UTC
    %Options will only be filled with parameters from the command line; everything else is left alone. If you need to provide default values, initialise them in %Options:
    my %Options = ("Verbose" => 0, User => 'user', Password => 'password', + ...); # other code
      The problem is that my command line parameters are not getting into %Options. Does GetOptions require a leading '-' or '--' on the command line parameters?
        Either should work. Post how you are calling your script, including the parameters.