Updated April 20, 2023
Introduction to Perl GetOptions
In Perl, GetOptions() is defined as a function that is an extended function of Getopt::Long module which is mainly for parsing the command line using various options and this function uses functions that have long names instead of characters which are declared using a double dash (–). In general, GetOption() can be defined as a function for supporting to run a combination of options, where this function can be called with the list of options having two parts in the options such option specifier that specifies the name of the option and option linkage is referring to the variable that needs the values to be set using the options specifier or option name.
Working of GetOptions() in Perl
In this article, we will discuss the GetOptions() function that is used for parsing the code that uses the coding such as command-line kinds of lines such as that uses various options to execute a particular task. Similarly, this GetOptions() function uses not only a single option but it can use more than one option in its command lie and hence it is called the extension of Getopt::Long module as it uses multiple options in its command and the options has two parts option specifier and option linkage as discussed above.
Now let us see syntax and examples for GetOption() in Perl.
Syntax:
$x = GetOptions( option_description);
In the above syntax, we are using GetOptions() and storing the result in variable “x” and we are passing the parameter as option description which has two parts such as option specifier that is used to specify the option name and option linkage which is used for linking or referring to a variable that is set whenever an option is present this option linkage is optional and can refer to scalar values, array and also subroutines.
There are few commonly used option specifiers such as
- option_name: this is used for indicating that the option is present and will set it to 1.
- Option_name!: this indicates that the negation declaration is allowed such as –option_name and if no name then it will be set to 0.
- Option_name+: incrementing of the variable for each occurrence of this option
- Option_name=s: this is used to indicate that option requires string value only
This function returns Boolean values such as true if the command line is executed successfully else it will throw an error message using functions like die() and warn() that returns false.
Examples
Let us discuss examples of Perl GetOptions.
Example #1
Code:
#!/usr/bin/Perl
use Getopt::Long;
print "\n";
print "Demonstration of GetOptions() in Perl is as follows";
print "\n";
print "\n";
$x = GetOptions ( "Python" => \$Python,
"Java" => \$Java,
"Perl" => \$Perl,
"PHP" => \$PHP );
print "The option is set as: ";
print $x;
print "\n";
print "\n";
$Python? print "Python is available \n": print "Python is not available \n";
$Java ? print "Java is available \n" : print "Java is not available \n";
$Perl ? print "Perl is available \n" : print "Perl is not available \n";
$PHP? print "PHP is available \n" : print "PHP is not available \n";
Output:
In the above program, we can see first to use GetOptions() function in the code we need to include or import Getopt:: Long module, and this is done by using the “use” keyword in Perl. Then we declare a variable using “$x” this stores the value in Boolean value such as 1 if true where it indicates the options are set successfully and 0 if the options are not set successfully false. Then we will be printing the option names are set with option linkage by referring to the options which will print available or not available of the course given in the above declaration of option names or in the option description. Then we are also printing the value that will be returned by the GetOptions() function which is a Boolean value which is done by printing the “$x” value which results in “1”. In the above output screenshot, we are trying to print the option as “—Ruby” which is not declared at all in the program and when we try to execute the code with this option then it results as saying it is not defined such as “Undefined option: ruby”. This code’s output can be seen in the above screenshot.
Now we will see in GetOptions() we can use the single character as options passing as a parameter to this function in the below example.
Example #2
Code:
use strict;
use warnings;
use 5.010;
use Getopt::Long qw(GetOptions);
print "Demonstartion od GetOptions() that can use single character as options";
print "\n";
print "The below prints if the option is set or no and also prints the name set to this";
print "\n";
print "\n";
my $data1;
my $name1 = 'Educba';
GetOptions(
'from=s' => \$name1,
'debug' => \$data1,
) or die "Usage: $0 --debug--from NAME\n";
say $data1 ? 'done' : 'not done';
if ($name1) {
say $name1;
}
Output:
In the above program, we have declared two variables such as “data1” and “name1” which is set to as “Educba” and “data1” gives the result if the options are set or not by displaying “done” or “not done” and we have set the option_name as for data1 we are specifying as “from” which is of string type and specifying data1 as “debug”. So here when we are writing a command in the command line we can need not use complete words like “from” or “debug” we can only use single characters such as –f and –d or half of the string such as –fr and –deb. The output of this code can be seen in the above screenshot.
Conclusion
In this article, we conclude that GetOptions() is a function that is used for specifying the functions and can be used with multiple options instead of using single options where this function needs to imported by getopt::Long module and hence this function is used for parsing command-line options in Perl. In this article, we saw a simple example of how to declare the GetOptions() function and the parameters that would be passed to it. In this article, we also saw we can use single characters as option names in the command line which is provided GetOptions().
Recommended Articles
This is a guide to Perl GetOptions. Here we also discuss the Definition and Working of GetOptions() in Perl along with different examples and its code implementation. You may also have a look at the following articles to learn more –