|
Contributed by Chad Brandt
|
|
|
|
Friday, 02 July 2004
Most of the perl programs I write depend on parameters passed in. An easy way to get parameters in a perl program is to use the Getopt functions. Here is an example program that shows how you would use Getopt to get the parameters passed to your program
#!/usr/bin/perl ######################################################## # Example perl program using Getopt to get command line # arguments passed to a program. # # This example can take 3 parameters # -h --help # -v --verbose # -t=n --timeout=n # # using Getopt::Long the -t value can be passed in the # following ways for a timeout of 5 # # -t5 # -t=5 # -t 5 # --timeout 5 # --timeout=5 # ######################################################## use Getopt::Long;
# initialize the parameter options $help = ''; $verbose = ''; $timeout = 0;
#call the function passing the param name and variable to assign the value to
GetOptions ('h|help' => \$help, 'v|verbose' => \$verbose, 'i|timeout=i' => \$timeout);
if ($help){ print "The help parameter was supplied to the programn"; }
if ($verbose){ print "The verbose parameter was supplied to the programn"; }
if ($timeout == 0){ print "timeout was not suppliedn"; } else { print "timeout set to $timeoutn"; }
exit(0);
Powered by AkoComment 1.0 beta 2! |