I'm working on a Perl script. How can I pass command line parameters to it?
Example:
script.pl "string1" "string2"
This question is related to
perl
command-line
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.
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 sub
s. Within a sub
, it will retrieve the list of arguments passed to the sub
rather than those passed to the program.)
foreach my $arg (@ARGV) {
print $arg, "\n";
}
will print each argument.
Yet another options is to use perl -s, eg:
#!/usr/bin/perl -s
print "value of -x: $x\n";
print "value of -name: $name\n";
Then call it like this :
% ./myprog -x -name=Jeff
value of -x: 1
value of -name: Jeff
Or see the original article for more details:
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 sub
s. Within a sub
, it will retrieve the list of arguments passed to the sub
rather than those passed to the program.)
Alternatively, a sexier perlish way.....
my ($src, $dest) = @ARGV;
"Assumes" two values are passed. Extra code can verify the assumption is safe.
my $output_file;
if((scalar (@ARGV) == 2) && ($ARGV[0] eq "-i"))
{
$output_file= chomp($ARGV[1]) ;
}
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.
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.
Alternatively, a sexier perlish way.....
my ($src, $dest) = @ARGV;
"Assumes" two values are passed. Extra code can verify the assumption is safe.
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.
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.
foreach my $arg (@ARGV) {
print $arg, "\n";
}
will print each argument.
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 sub
s. Within a sub
, it will retrieve the list of arguments passed to the sub
rather than those passed to the program.)
Yet another options is to use perl -s, eg:
#!/usr/bin/perl -s
print "value of -x: $x\n";
print "value of -name: $name\n";
Then call it like this :
% ./myprog -x -name=Jeff
value of -x: 1
value of -name: Jeff
Or see the original article for more details:
foreach my $arg (@ARGV) {
print $arg, "\n";
}
will print each argument.
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.
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 sub
s. Within a sub
, it will retrieve the list of arguments passed to the sub
rather than those passed to the program.)
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.
Source: Stackoverflow.com