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

O, Ye who are faithful, I beseech thee aid this lowly initiate... For some reason I don not understand, I get an 'unitialized value' warning everywhere $args is used...even when I pass in arguments. I'm sure is some simple thing I've overlooked, but no idea what that simple thing is.
#!/usr/bin/perl -wT use Getopt::Std; use Getopt::Long; GetOptions( "help" => \$help, "file=s" => \$file, "database=s" => \$database, "user=s" => \$user, "password=s" => \$password); #-h ARG, -f ARG, -d ARG, -u ARG, -p ARG sets $args{h},sets $args{f}, s +ets $args{d}, sets $args{u}, sets $args{p} getopts("hf:d:u:p:", \%args); if ($args>=2){ $help=$args{h}; $file=$args{f}; $database=$args{d}; $user=$args{u}; $password=$args{p}; }elsif ($help){ print "rip_sched -h/--help -f/--file datafile -d/--database databa +se -u/--user database-user -p/--password database-password\n"; exit 0; }

Replies are listed 'Best First'.
Re: getopt?
by tommyw (Hermit) on Oct 25, 2001 at 02:28 UTC

    First problem: the comparison of $args with 2. What is $args? It's not related to the %args hash that the single character options have been loaded into. Unfortunately, I can't figure out what you actually want to test. So that's the first warning you're getting. And with the code you've posted, that's the only one.

    After that, you need to note that the parameters you pass cause the appropriate variables to be set, and only those variables. So adding a diagnostic at the end of:

    print "help=$help\n", "file=$file\n", "db=$database\n", "user=$user\n", "password=$password\n";
    and then passing a parameter --file fred will still cause complaints about all the other values being unset.

    Finally, and possibly most importantly, GetOptions destroys your @ARGV. So getopts will never have anything to read parameters from

      thanks, I _really_ misread something there.
      I'm using scalar(keys %args) instead of $args, now.
      Is there anyway to use both getopts and GetOptions so that you can code for either GetOpt::Long or GetOpt::Std ?

        Brute force and ignorance suggests:

        @ARGV2=@ARGV; GetOptions(...); @ARGV=@ARGV2; getopts(...);
        But note that GetOptions allows you to abbreviate the commands as long as their uniquely identifiable. So you can pass --f instead of --file, and therefore don't necessarily need getopts, provided you're willing to live with the double hyphen.

        PS. I still don't understand the lest for the size of %args as an alternative to testing $help. If you supply 2 single hyphen parameters (possibly including -h) then you don't get the help message?