Wednesday, 9 September 2020

perl GetOptions tutorial

Getopt::Long module in Perl to parse command line options and arguments.

Getopt::Long is a Perl module that provides a way to parse command line options and arguments in a flexible and powerful way. It supports both short and long options, as well as optional and mandatory arguments.

Here is an example code snippet that demonstrates how to use Getopt::Long:

use strict;

use warnings;

use Getopt::Long;


my $name;

my $age;

my $verbose;


GetOptions(

    "name=s"    => \$name,     # string option

    "age=i"     => \$age,      # integer option

    "verbose"   => \$verbose,  # flag option

) or die "Error in command line arguments\n";

if ($verbose) {

    print "Name: $name\n" if defined $name;

    print "Age: $age\n" if defined $age;

}


In this example, we first declare three variables $name, $age, and $verbose to hold the values of the options we will parse. We then call the GetOptions() function to parse the command line options.

The first argument to GetOptions() is an anonymous hash that defines the options we want to parse. The key is the option name, and the value is a reference to the variable that will hold the option value.

For example, the "name=s" option specifies a string option named name. The =s part tells GetOptions() to expect a string argument after the name option. The => operator assigns the value of the option to the $name variable.

Similarly, the "age=i" option specifies an integer option named age. The =i part tells GetOptions() to expect an integer argument after the age option.

The "verbose" option specifies a flag option, which does not take any argument. If the --verbose option is present on the command line, the $verbose variable will be set to a true value.

Finally, we use the $verbose variable to determine whether to print the parsed options. If $verbose is true, we print the values of $name and $age if they are defined.

The GetOptions() function returns a true value if it successfully parses the options, or false if there is an error. In this example, we use the or die syntax to print an error message and exit the script if there is an error in the command line arguments.

Example 2:


use Getopt::Long;

my $name;
my $age;

GetOptions("name=s" => \$name, "age=i" => \$age);

print "Name: $name\n";
print "Age: $age\n";

In this example, we are using GetOptions to parse command line arguments. The Getopt::Long module provides the GetOptions function which takes a list of option specifications and a reference to a hash or a variable where the options and their values will be stored.

In this example, we define two variables $name and $age that will hold the values of the name and age options respectively. We then call GetOptions with two option specifications: "name=s" and "age=i". The s and i indicate that the name option is a string and the age option is an integer.

The backslash (\) before the variables tells GetOptions to store the values in those variables. If we didn't provide the backslash, GetOptions would print the values to STDOUT and exit the script.

We can run the script with the following command:

$ perl script.pl --name "John Doe" --age 30

This will output:

Name: John Doe 
Age: 30

Note that the order of the options doesn't matter. We could have provided the --age option before the --name option and it would still work the same way.

Labels: