Skip to content
Blog Perl: Simple way to read Command line arguments

Perl: Simple way to read Command line arguments

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";
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.