in reply to getopt?

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

Replies are listed 'Best First'.
Re: Re: getopt?
by azool (Initiate) on Oct 25, 2001 at 02:57 UTC
    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?

        Actually, you don't even need to live with the double hyphen. Single hyphen is also parsed by Getopt::Long. Note that clustering does not work, however: -rex gets interpreted as one option, 'rex' (Update: (thanks chipmunk!) unless you turn them on). But otherwise you can use single-letter options as long as they are distinct. If you really want to do this sort of option-parsing, though, try Getopt::Mixed.
        Yes, you're right. I realized that last night and fixed it.
        now, it reads...
        if ($help || !($file && $database && $user && $password)){ print "rip_sched -h/--help -f/--file datafile -d/--database database - +u/--user database-user -p/--password database-password\n"; exit 0; }