Thursday, January 6, 2011

Reading command line arguments with @ARGV array in Perl


@ARGV is a special array in Perl, it holds the arguments passed to the program 
as each element of the array. Therefore $ARGV[0] contains the first argument and 
$ARGV[1] contains the second argument etc.
Following is an example of accepting the name and age of the end user as command 
line arguments and displaying the same as a well formed sentence:

#!/usr/bin/perl
#---------------------#
#  PROGRAM:  test_argv.pl  #
#---------------------#

print "My name is $ARGV[0] and I am $ARGV[1] years old\n";

Running the program from command line:
--------------------------------------
perl test_argv.pl James 33

Output:
-------
My name is James and I am 33 years old