I am using Getopt::Std
in a Perl script, and would like to pass in a zero as value. I am checking that values are set correctly using unless()
. At the moment unless()
is rejecting the value as being unset.
Is there a way to get unless()
to accept zero as a valid value (any non-negative integer is valid).
This is probably perfeclty simple, but I've never touched Perl before a few days ago!
Rich
Like the option c in the above program. one can easily mess-up , if you happen to use that in your program. (yes there are ways to avoid it) The options passed to getopt function doesn't seem very nice. abc:d tells c is a option where some parameter is expected. You need to have your own logic to multiplex various arguments.
getopt() function in C to parse command line arguments. The getopt() function is a builtin function in C and is used to parse command line arguments. Syntax: getopt(int argc, char *const argv[], const char *optstring) optstring is simply a list of characters, each representing a single character option.
If a letter is followed by a colon, the option is expected to have an argument, or group of arguments, which must be separated from it by white space. In this sample script we will take single argument as an input to our script using getopts. The script currently only supports -h as input argument which will show the usage function
If arg is not present, getopts processes the command-line arguments. If optstring starts with a colon (: ), the script must take care of generating error messages; otherwise, getopts generates error messages. The getopts builtin uses the OPTIND (option index) and OPTARG (option argument) variables to track and store option-related values.
You need to use unless defined <SOMETHING>
instead of unless <SOMETHING>
, because zero is false in Perl.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With