143

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

Example:

script.pl "string1" "string2"
1
  • 3
    Well, first off, you're going to need for it to be ./script.pl or a whole lotta nothin' will be happening come runtime. Commented Jul 21, 2014 at 19:59

9 Answers 9

194

It depends on what you want to do. If you want to use the two arguments as input files, you can just pass them in and then use <> to read their contents.

If they have a different meaning, you can use GetOpt::Std and GetOpt::Long to process them easily. GetOpt::Std supports only single-character switches and GetOpt::Long is much more flexible. From GetOpt::Long:

use Getopt::Long;
my $data   = "file.dat";
my $length = 24;
my $verbose;
$result = GetOptions ("length=i" => \$length,    # numeric
                    "file=s"   => \$data,      # string
                    "verbose"  => \$verbose);  # flag

Alternatively, @ARGV is a special variable that contains all the command line arguments. $ARGV[0] is the first (ie. "string1" in your case) and $ARGV[1] is the second argument. You don't need a special module to access @ARGV.

Sign up to request clarification or add additional context in comments.

2 Comments

$ARGV[0] is the first argument; perl counts from 0, and the program name is in $0 not @ARGV.
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.

3 Comments

It's also worth noting that in some programming languages the first (0) argument is the command or script itself... not so in perl though, of course.
Instead of $#ARGV + 1 you could also have said @ARGV
just use ARGV[0] or $argv[1] if you are looking for particular argument.
31
foreach my $arg (@ARGV) {
    print $arg, "\n";
}

will print each argument.

1 Comment

If not using getopts, this is how I would recommend non-destructively traversing an argument list. Based on perlmeme.org/howtos/syntax/foreach.html it looks like the syntax is correct; for a caveat, check the section, Side-effects : The control variable is an alias to the list element
25

Yet another option is to use perl -s, e.g.:

#!/usr/bin/perl -s

print "value of -x: $x\n";
print "value of -name: $name\n";

Then call it like this (as root):

./myprog -x -name=Jeff

Output:

value of -x: 1
value of -name: Jeff

Or see the original article for more details.

1 Comment

If using switches with -e you need to add -- before your switches like perl -se 'print "got $some\n"' -- -some=SOME. from here
23

Alternatively, a sexier perlish way.....

my ($src, $dest) = @ARGV;

"Assumes" two values are passed. Extra code can verify the assumption is safe.

1 Comment

It is not sexier. All arguments are available through @ARGV without any action from you just like C/C++ supply them through argv/argc
12

You can access them directly, by assigning the special variable @ARGV to a list of variables. So, for example:

( $st, $prod, $ar, $file, $chart, $e, $max, $flag ,$id) = @ARGV;

perl tmp.pl 1 2 3 4 5

enter image description here

1 Comment

Please review Why not upload images of code/errors when asking a question? (e.g., "Images should only be used to illustrate problems that can't be made clear in any other way, such as to provide screenshots of a user interface.") and do the right thing (it covers answers as well).
7

If the arguments are filenames to be read from, use the diamond (<>) operator to get at their contents:

while (my $line = <>) {
  process_line($line);
}

If the arguments are options/switches, use GetOpt::Std or GetOpt::Long, as already shown by slavy13.myopenid.com.

On the off chance that they're something else, you can access them either by walking through @ARGV explicitly or with the shift command:

while (my $arg = shift) {
  print "Found argument $arg\n";
}

(Note that doing this with shift will only work if you are outside of all subs. Within a sub, it will retrieve the list of arguments passed to the sub rather than those passed to the program.)

Comments

5

Use:

my $output_file;

if ((scalar (@ARGV) == 2) && ($ARGV[0] eq "-i"))
{
    $output_file = chomp($ARGV[1]);
}

Comments

2

If you just want some values, you can just use the @ARGV array. But if you are looking for something more powerful in order to do some command line options processing, you should use Getopt::Long.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.