Perl Command Line Arguments

Command line arguments are sent to a Perl program in the same way as in any other language. The @ARGV array holds the command line argument. There is no need to use variables even if you use "use strict". By default, this variable always exists and values from the command line are automatically placed inside this variable. To access your script's command-line arguments, you just need to read from @ARGV array.

Perl allows using @ARGV array as filenames by using <>. The $ARGV contains the name of the current file when reading from <>.

@ARGV array elements

The @ARGV array works same as a normal array. Its first argument will be $ARGV[0], second $ARGV[1], and so on.

Example

Let's see a simple example to print command line arguments. In this example, we will print a welcome message with the users name as the argument from the command line. We need two command line arguments as user's first and last name.

Step1 Create a script welcome.pl in your system.

snippet
#!/usr/bin/perl -w
# (1) check for the number of command-line arguments entered
$number_args = $#ARGV + 1;
if ($number_args != 2) {
    print "Wrong entry. Please enter your full name.\n";
    exit;
}
# (2) if two command line arguments received,
$firstName=$ARGV[0];
$lastName=$ARGV[1];
print "Welcome $firstName $lastName at rookienerd.\n";

In the above script, first it will check whether two command line arguments are provided or not. If not, it will give the wrong entry error. And if yes, it will process those arguments as $ARGV[0] and $ARGV[1] respectively.

Step2 Run the script in your console as perl welcome.pl Christian Grey

Note
Note: Before giving this command, make sure you are on the directory containing your script welcome.pl
Perl Command line arguments 1

This is the output you will get in your console.

Step2 Run the script in your console as perl welcome.pl Christian

Here, we are passing only one argument from the command line.

Perl Command line arguments 2

In the output, you can see the wrong entry message for passing one argument.

Command Line Arguments using Loop

Now we will run a loop to print the command line arguments. In this example, you can enter as much argument as you wish.

Step1 Create a script loop.pl in your system.

Example
snippet
#!/usr/bin/perl
$get_args = $#ARGV + 1;
print "Total command line arguments received: $get_args\n";
foreach $argument (0 .. $#ARGV) {
    print "$ARGV[$argument]\n";
}

Step2 Run the script in your console as perl loop.pl a b c d e f g h

Perl Command line arguments 3

Look at the output above, it also displays total arguments passed on command line. Here we have passed 8 arguments.

Perl Getopt::Long

The simple command line options are done using ?s option. Complex command line options are done using Getopt::Std and Getopt::Long.

Getopt stands for GetOptions. It processes the content of @ARGV based on the configuration we give to it. It returns true or false value based on the processing.

Example

In this example, we well get the age of the user from the command line.

snippet
use strict;
use warnings;
use 5.010;
use Getopt::Long qw(GetOptions);
my $x;
GetOptions('from=s' => \$x) or die "Usage: $0 --from NAME\n";
say"According to your age which is $x:";
if ($x >=18) {
   say "You are eligible to vote";
}

We have declared a variable $x which will store value inserted into --from from the standard console. The from=s declares command line parameter called --from with a string after it. It is mapped further to the variable $x. The backslash (\) means we are passing a reference to the variable.

This script will switch to die part only when we will run this script by passing something that looks like a parameter name and starts with a (-) but is not declared in this script.

Now, we will run this program.

Perl Command line arguments 4

Look at the output, after entering age on the console, we got the above output.

Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +