How can I pass command-line arguments to a Perl pr

2019-01-04 22:26发布

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

Example:

script.pl "string1" "string2"

9条回答
我只想做你的唯一
2楼-- · 2019-01-04 23:03

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.

查看更多
Lonely孤独者°
3楼-- · 2019-01-04 23:06

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.

查看更多
男人必须洒脱
4楼-- · 2019-01-04 23:11

Alternatively, a sexier perlish way.....

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

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

查看更多
登录 后发表回答