How to handle with command line argument values in Perl

Perl command line arguments stored in the special array called @ARGV.

ARGV example

Use $ARGV[n] to display argument.

Use $#ARGV to get total number of passed argument to a perl script.

For example, if your scriptname is foo.pl and you called script as follows:

./foo.pl one two three

You can print one, two, three command line arguments with print command:

print “$ARGV[$0]\n”;
print “$ARGV[$1]\n”;
print “$ARGV[$2]\n”;

Or just use a loop to display all command line args:

#!/usr/bin/perl -w
foreach $num (0 .. $#ARGV) {
print “$ARGV[$num]\\n”;
}

Advertisements
Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: