Alex Rivera | Logout

How can I parse command-line arguments in a Perl program?

Asked 2008-12-12T03:05:06.163
143

I'm working on a Perl script. How can I parse command line parameters given to it?

Example:

script.pl "string1" "string2"
Edit
Report

1 Answer

64

You pass them in just like you're thinking, and in your script, you get them from the array @ARGV. Like so:

my $numArgs = $#ARGV + 1;
print "thanks, you gave me $numArgs command-line arguments.\n";

foreach my $argnum (0 .. $#ARGV) {

   print "$ARGV[$argnum]\n";

}

From here.

answered 2008-12-12T03:07:34.253

Your Answer