Processing Command Line Options with PERL - GetOptions() Function (Page 4 of 9 )
Most of the magic here lies in the call to the GetOptions() function. This function accepts a series of option-variable pairs, demarcated using standard hash notation and separated with commas. When GetOptions() is called, it reads the program command line, looks for matching arguments, and if found, sets the corresponding option variable to true. Thus when you call the Perl script above with the "--debug" option, GetOptions() recognizes it and automatically sets the $debug variable to true.
You can set more than one option variable at a time as well. Consider the following example and its output, which demonstrates:
#!/usr/bin/perl
# import module
use Getopt::Long;
# read options
$result = GetOptions ( "red" => $red,
"blue" => $blue,
"green" => $green );
# print options
$red ? print "Red is presentn" : print "Red is absentn"; $blue ? print
"Blue is presentn" : print "Blue is absentn"; $green ? print "Green is
presentn" : print "Green is absentn";
Here's an example of the output:
$ ./colors.pl --red --blue
Red is present
Blue is present
Green is absent
Next: Half-Life >>
More Perl Articles
More By icarus, (c) Melonfire