Getopt::Long - Handle command line options
You can use Getopt::Long to handle options for command line arguments.
# Loading modules and importing functions use Getopt::Long 'GetOptions';
Consider specifying the following options with command line arguments:
# Specify the option with the command line argument. perl script.pl - enable_cache - max_clients = 5 - type = single config.init
You can easily handle such command line options using the GetOptions function .
# Handling command line argument options with GetOptions # Set default value my $enable_cache; my $max_clients = 5; my $type = 'prefork'; # Optional processing GetOptions( 'enable_cache' => \$enable_cache, 'max_clients = i'=> \$max_clients, 'type = s'=> \$type ); Receives non-option arguments my $conf_file = shift;
How to use the GetOptions function.
# Arguments of GetOptions GetOptions( 'Option name = Value type' =>'Variable to save', 'Option name = Value type' =>'Variable to save', ... );
For "Option name = Type", specify the option name and value type. If the option name is a boolean value, the value type is omitted. If the option name is an integer, specify i as the value type. If the option name is a string, specify s as the value type.
# Authenticity value 'enable_cache' => \$enable_cache # Integer 'max_clients = i'=> \$max_clients # String 'type = s'=> \$type
For "Variable to save", specify a reference for a scalar variable. Set the default value first and pass it as a reference to the GetOptions argument.
# Set default value my $max_clients = 5; # Specify a scalar variable reference in the argument GetOptions( 'max_clients = i'=> \$max_clients, );
Command line options can also be specified by abbreviated names. The initial letter of the option name is the abbreviated name.
# Specify command line argument options with abbreviated name perl script.pl -e -m = 5 -t = single config.init
= Can be omitted.
# Omit equal perl script.pl - enable_cache - max_clients 5 - type single config.init