Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass zero in to Getopt::Std

Tags:

perl

getopt

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 image 459
Rich Avatar asked Sep 07 '10 15:09

Rich


People also ask

Is there a way to avoid using getopt function?

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.

What is getopt in C with example?

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.

How to take single argument as an input to getopts?

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

What happens if there is no arg in getopts?

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.


1 Answers

You need to use unless defined <SOMETHING> instead of unless <SOMETHING> , because zero is false in Perl.

like image 168
DVK Avatar answered Sep 22 '22 09:09

DVK