Friday 21 February 2014

Getopts Tutorial - Command line options in Perl

In Perl, if we want the user to pass certain options at command line, we can define the switches in the program code using Getopts. Following is a brief tutorial underlying it's usage with a practical example.

First of all, search the CPAN for Getopt and you'll see a lot of available modules - each with a slightly distinct functionality. All serve the same purpose though - providing a framework for passing user-defined command line options.

Now, install either the Getopt::Long or Getopt::Compact or Getopt::Lucid module from CPAN. I assume you know how to install modules from the terminal. If not, dig in through this blog. You'll get the answers.

In this example, I'll use the highly rated Getopt::Long module. Make sure you refer the module's official documentation link for more details. The following illustration is about it's simple basic usage, just to give you a feel. I'll cover an advanced usage tutorial for this module in another post.

Ok guys, enough talk. Lets code some stuff. Here we go...

use strict;
use warnings;
use Getopt::Long;

my $name;
my $location;

GetOptions (

               'name=s' => \$name,
               'location=s' => \$location,
);

if ($name) {
          print "Welcome $name \n";
}

if ($location) {
          print "Hows the weather at $location \n";

}

Here, "name" and "location" are the options to which we pass an argument. Note that we have mentioned name=s and location=s. The alphabet 's' implies the switch will be of string type. If you want the switch to be of integer type, use i. ( Eg : 'age=i' => \$age ).

Also, the equal sign = indicates that it is a mandatory option. If you want an option to be optional, use the colon : sign ( Something like - 'help:h' => \$help ).

Save the program as Getopts_Ex.pl and now open the terminal. From the command line, run the program as follows :

perl Getopts_Ex.pl -name "IronCladZone" -location "New York"

The output would be as follows :


Note that you have to pass an argument for each mandatory option. If you don't pass an argument, you will get an error. You can also play around by defining a separate function with customized error messages or switches' usage. This can be extremely helpful to a newbie or a layman, informing him how to run the program and how to use the custom switches defined in the script.

The above code contains print statements within the "if" block. You can instead tweak the "if" block to call custom user-defined functions a.k.a subroutines from here and put the print codes in the subroutine. The output will be the same, but the below structure makes it more graceful and secure. In fact, I plan to cover the topic of subroutines in another fresh post. Stay tuned for that as well.

use strict;
use warnings;
use Getopt::Long;

my $name;
my $location;

GetOptions (

               "name=s" => \$name,
               "location=s" => \$location,
);

if ($name) {
          func_name();
}

if ($location) {
          func_location();

}


sub func_name()  {
            print "Welcome $name \n";
}

sub func_location()  {
            print "Hows the weather at $location \n";
}

Folks, do let me know your comments about this post and suggestions on how to improve the quality of the content. Ciao.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...
eXTReMe Tracker