in reply to Getopt::Std and simple options

Are you trying to use  -v something like a verbose flag?

You might want to use getopts() instead of getopt() as the latter requires the swtich to have an arg (i.e. you need to use it as scriptname -v somearg) getopts() provides this by : to the end of switch.

Also you should check for return value from getopt/s(). If you had checked for the return value you would have seen  broken usage with your code as is i.e. getopt()

use strict; use Getopt::Std; our $opt_v; # print join(':',@ARGV), "\n"; my $ret = getopts('v'); die "broken usage\n" unless $ret; print "V = ", $opt_v,$/ if defined $opt_v; __END__ V = 1

Replies are listed 'Best First'.
Re^2: Getopt::Std and simple options
by Dizzley (Novice) on Sep 06, 2005 at 05:39 UTC
    Yes, very astute, I am trying to set a verbose flag. :)

    Thanks for your helpful reply. I should have used getopts() as getopt only takes opts with a value. I will take on board the other comments.

    ~Diz

      Checking the return value from each of the Getopt::* variants is what makes these functions really useful. An idiom that I use almost constantly for command-line tool scripts goes like this:
      my $Usage = "Usage: $0 -a [-b arg] whatever ...\n"; getopts( ... ) or die $Usage;
      When I need a reminder about the command line syntax for one of my scripts (this happens a lot), I just run it with "-?" as the only arg; since I never use "?" as an option flag, I always get the usage message.