in reply to Making Getopt::Long tell me if no parameters passed

At the very bottom of the docs in the troubleshooting section you will find the following line:

GetOptions does not return a false result when an option is not supplied
That's why they're called 'options'.

What you might want to try is to set a default variable to 0 that is changed to a 1 when any option is passed. (or vice versa), and then calling the usage sub appropriately.

-enlil

  • Comment on Re: Making Getopt::Long tell me if no parameters passed

Replies are listed 'Best First'.
Re: Re: Making Getopt::Long tell me if no parameters passed
by ibanix (Hermit) on Dec 17, 2002 at 04:06 UTC
    Thanks.

    I decided to go with
    &usage if not defined $user; &usage if not defined $pass; &usage if not defined $config;
    until I could figure out something more elegant.

    Cheers,
    ibanix

    $ echo '$0 & $0 &' > foo; chmod a+x foo; foo;
      fever pointed out the right thing to do, but just for completeness' sake, your approach could be condensed, too. The obvious, naive version: &usage unless (defined $user) or (defined $pass) or (defined $config); And the Once And Only Once version: &usage unless grep defined, $user, $pass, $config; That said, go with passing GetOptions a hashref.

      Makeshifts last the longest.