Perl FAQ: How do I read command-line arguments in Perl?
If you want to handle simple Perl command line arguments, such as filenames and strings, this tutorial is for you. If you want to handle command-line options (flags) in your Perl scripts (like "-h" or "--help"), this new Perl getopts command line options/flags tutorial is what you need.
With Perl, command-line arguments are stored in a special array named @ARGV. So you just need to read from that array to access your script's command-line arguments.
ARGV array elements: In the ARGV array, $ARGV[0] contains the first argument, $ARGV[1] contains the second argument, etc. So if you're just looking for one command line argument you can test for $ARGV[0], and if you're looking for two you can also test for $ARGV[1], and so on.
ARGV array size: The variable $#ARGV is the subscript of the last element of the @ARGV array, and because the array is zero-based, the number of arguments given on the command line is $#ARGV + 1.
A typical Perl script that uses command-line arguments will (a) test for the number of command line arguments the user supplied and then (b) attempt to use them. Here's a simple Perl script named name.pl that expects to see two command-line arguments, a person's first name and last name, and then prints them:
#!/usr/bin/perl -w
# (1) quit unless we have the correct number of command-line args
$num_args = $#ARGV + 1;
if ($num_args != 2) {
print "\nUsage: name.pl first_name last_name\n";
exit;
}
# (2) we got two command line args, so assume they are the
# first name and last name
$first_name=$ARGV[0];
$last_name=$ARGV[1];
print "Hello, $first_name $last_name\n";
This is fairly straightforward, where adding 1 to $#ARGV strikes me as the only really unusual thing.
To test this script on a Unix/Linux system, just create a file named name.pl, then issue this command to make the script executable:
chmod +x name.pl
Then run the script like this:
./name.pl Alvin Alexander
Or, if you want to see the usage statement, run the script without any command line arguments, like this:
./name.pl
For a second example, here's how you might work through the command line arguments using a Perl for loop:
#!/usr/bin/perl
#---------------------#
# PROGRAM: argv.pl #
#---------------------#
$numArgs = $#ARGV + 1;
print "thanks, you gave me $numArgs command-line arguments:\n";
foreach $argnum (0 .. $#ARGV) {
print "$ARGV[$argnum]\n";
}
To demonstrate how this works, if you run this Perl command line args program from a Unix command-line like this:
./argv.pl 1 2 3 4
or, from a DOS command-line like this
perl argv.pl 1 2 3 4
you'll get this result:
thanks, you gave me 4 command-line arguments: 1 2 3 4
As you can see, it prints all the command line arguments you supply to the Perl program.
Very helpful - thanks!
Very helpful - thanks!
alternatives
You can also use this:
instead of:
You can use Perl map
Also you can use the Perl map operator:
map { print "$_\n" } @ARGV;instead of:
foreach $argnum (0 .. $#ARGV) { print "$ARGV[$argnum]\n"; }Thanks for the Perl map
Thanks for the Perl map reminder. For some reason I can never remember to use it, and it's much easier than the for loop I have shown.
for(@ARGV){ print
for(@ARGV){
print "$_\n";
}
...is also quite easy ;-)
Great ! Thanks
Thanks !
You can also use: foreach
You can also use:
foreach $argnum (@ARGV)
Shift
I find it easier most of the time to use shift:
my variable = shift;
will read the next command line argument.
Post new comment