Perl: Simple way to read Command line arguments

On December 14, 2010, in PERL, by Dipin Krishna

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";
}
Tagged with:  

Leave a Reply

*

Search