Site icon Dipin Krishna

Perl: Simple way to read Command line arguments

perl

Parsing command-line arguments in Perl is very easy.

All the command-line arguments are present in the array $ARGV. You can get the number of arguments passed as ( $#ARGV + 1 ).

Below is a sample Perl script to read the command-line parameters.

#!/usr/bin/perl
#-------------------------------------------------#
#  File:    argv.pl                               #
#  Author:  Dipin Krishna mail@dipinkrishna.com   #
#-------------------------------------------------#
# The commandline arguements are present in the variable $ARGV.
 
$numberofArgs = $#ARGV + 1;
print "The $numberofArgs command-line arguments are:\n";
 
foreach $argnum (0 .. $#ARGV) {
    print " $argnum.\t$ARGV[$argnum]\n";
}
Exit mobile version