Perl

Perl Command Line Arguments Processing

The command line argument values are passed at the time of the script execution. The @ARGV array is used to read these values in Perl. Each value can be accessed by mentioning the index of the @ARGV array. The index of the array starts from 0. The total number of the command line arguments can be calculated using $#ARGV. The methods of passing the command line arguments and accessing the argument values are shown in this tutorial.

Syntax:

The syntax of passing the command line arguments is shown in the following. Each argument value is given by a space after the script name during the execution:

perl script_name arg1 arg2…. argn

Perl Command Line Argument Examples

Different ways of using the Perl command line examples are shown in this part of the tutorial.

Example 1: Read the Arguments by the Index Value

Create a Perl file with the following script that takes a minimum of two command line arguments and calculate the sum of the first two argument values. Next, the result of the sum is printed.

#!/usr/bin/perl
use strict;
use warnings;

#Count the total number of command line arguments
my $total_arg = $#ARGV + 1;
#Check the number of arguments
if ($total_arg >= 2)
{
    print "Total number of arguments: $total_arg\n";
    #Read the first argument
    my $arg1 = $ARGV[0];
    #Read the second argument
    my $arg2 = $ARGV[1];
    #Calculate the sum of the first two arguments
    my $sum = $arg1 + $arg2;
    #Print the sum of argument values
    print "The sum of $arg1 and $arg2 is $sum\n";
}

Output:

The following output appears after executing the script. In the output, 10 and 5 are taken as argument values and the sum of these numbers is 15 which is printed in the output:

Example 2: Read the Argument Values Using the “Foreach” Loop

Create a Perl file with the following script that prints all argument values using the “foreach” loop. The $#ARGV variable is used to count the total number of argument values. Next, if the number of arguments is more than or equal to 1, the “foreach” loop iterates to read each argument value and prints it.

#!/usr/bin/perl

use strict;
use warnings;
use 5.34.0;

my $total_arg = $#ARGV + 1;
#Check the number of arguments
if ($total_arg >= 1)
{
    say "All argument values are:";

    #Print all argument values using the 'foreach' loop
    foreach my $index (0 .. $#ARGV)
    {
         print "$ARGV[$index] ";
    }

    print "\n";
}
else
{
    say "No argument value was given.";
}

Output:

The following output appears after executing the script two times. The script is executed without any argument the first time and “Perl”, “PHP”, and “Python” were taken as the argument values in the second execution:

Example 3: Read the Argument Values Using the “While” Loop

Create a Perl file with the following script that prints all argument values using the “while” loop. Like in the previous example, the total number of arguments is used to iterate the loop and prints the argument values.

#!/usr/bin/perl

use strict;
use warnings;
use 5.34.0;

#Count the total arguments
my $total_arg = $#ARGV + 1;
#Check the number of arguments
if ($total_arg >= 1)
{
        #Initialize the counter
        my $counter = 1;
        #Iterate the loop to read all argument values
        while ($counter <= $total_arg)
        {
              #Print each argument value
              say "Argument value $counter: $ARGV[$counter-1]";
              #Increment the counter
              $counter++;
        }
}
else
{
       say "No argument was provided.";
}

Output:

The following output appears after executing the script three times. In the first execution, the script was executed without any argument. In the second execution, the script was executed with one argument. In the third execution, the script was executed with three arguments:

Example 4: Calculate “X” to the Power “N”

Create a Perl file with the following script that takes the base and power value from the command line argument and print the output after calculating the power value.

#!/usr/bin/perl
use strict;
use warnings;
use 5.34.0;

#Check the number of argument values
if ($#ARGV + 1 >= 2)
{
      #Read the first argument and remove the newline
      my $x = $ARGV[0];
      chomp($x);

      #Read the second argument and remove the newline
      my $n = $ARGV[1];
      chomp($n);

      #Initialize the variable
      my $result = 1;
      #Calculate the power based on the input value
      for (my $i = 1; $i <= $n; $i++)
      {
            $result *= $x;
      }
      #Print the calculated value
      say "The $x to the power $n is $result.";
}
else
{
      #Print the error message
      say "The value of x and n were not provided.";
}

Output:

The following output appears after executing the script two times. No argument was given in the first execution. Two numeric arguments were given in the second execution:

Conclusion

Different uses of command line argument values are shown in this tutorial using multiple examples. This tutorial helps the Perl users to know the uses of command line arguments in the Perl script.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.