Processing Command Line Options with PERL - Getopt::Long.pm (Page 6 of 9 )
It is interesting to note that the equality symbol (=) sign between the option name and the data type specifier tells Getopt::Long.pm that an option value *must* be provided for the option. Look what happens if, for example, you specify the "--age" option without a corresponding integer value:
$ ./script.pl --age
Option age requires an argument
In order to make an option value optional (try saying that fast!), use a colon (:) instead of an equality symbol (=), as below:
#!/usr/bin/perl
# import module
use Getopt::Long;
# read options
$result = GetOptions ("age:i" => $age);
# print value
print "Input age is $age years";
Here, in the absence of a value for the option, Getopt::Long.pm will automatically assign 0 (for integer values) or an empty string (for string values) to the corresponding option variable. Take a look at the output of the script above to verify this:
$ ./script.pl --age
Input age is 0 years
Next: Opting In >>
More Perl Articles
More By icarus, (c) Melonfire