perl : Process Command Line Arguments

GetOpt module in perl provides a very easy way to process the command line arguments. It also performs the type check. Here is a sample snippet that I use very often.

use Getopt::Long;

#Process command line arguments
$result = GetOptions(
	"port=i"	=> $Port,
	"host=s"	=> $Host,
	"interval=f"	=> $Interval,
	"random!"	=> $Randomize,
	"h"		=> $Help
);

The type after the equal “=” sign dictates the type of variable that is expected. This is very helpful because the module will perform the type checking and ensure that it receives what is expected. For example if you try to give –port “abcd” it will fail that an integer is expected. Read on for a brief explanation of types. Continue reading “perl : Process Command Line Arguments”