views:

6707

answers:

6

I'm working on a Perl script. First time in more than a decade. Really.

How can I pass command line parameters to it?

Example:

script.pl "string1" "string2"
+8  A: 

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

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

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

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

}

From here.

George Stocker
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.
danieltalsky
Instead of `$#ARGV + 1` you could also have said `@ARGV`
Leon Timmermans
Yup. I went with an already posted example on the net. It was late and I wanted to get to bed. Also, TMTOWTDI! :-)
George Stocker
+7  A: 
foreach my $arg (@ARGV) {
    print $arg, "\n";
}

will print each argument.

nrich
When I try this code:syntax error at line 3 : `(' unexpected
lamcro
If not using getopts, this is how I would recommend non-destructively traversing an argument list. Based on http://www.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_
jaredor
My error. Forgot to put the exclamation(!) in the first line: #!/usr/bin/perl
lamcro
+17  A: 

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.

$ARGV[0] is the first argument; perl counts from 0, and the program name is in $0 not @ARGV.
derobert
despite the $ARGV[0] confusion .. this is the kind of answer I hope to find, when I search SO, thanks and +1 from me.
lexu
$ARGV variable is what I will use then. Thanks.
lamcro
+3  A: 

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.)

Dave Sherohman
+1  A: 

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.

Marc
A: 

Funny, I forgot to specify the datatype using Getopt (passing an integer, '=i') and the result was that a 0 became a 1, but a 1 remained a 1 and a 2 a 2 and so on.

Cheers

felix