您可以将命令行参数传递给 perl,它们将显示在特殊数组 @ARGV 中。
基本的命令行参数传递
# In bash
./perlScript.pl 123
# In perl
my ($num) = $ARGV[0]; # The first command-line parameter [ 123 ]
许多位置命令行参数
# In bash
./perlScript.pl 123 456 789 foo bar
# In perl
my ($n1,$n2,$n3,$str1,$str2) = @ARGV; # First 5 command line arguments will be captured into variables
许多命令行标志
# In bash
./perlScript.pl --min=123 --mid=456 --max=789 --infile=foo --outfile=bar
# In perl
use Getopt::Long;
my ($min,$mid,$max,$infile,$outfile,$verbose);
GetOptions(
"min=i" => \$min, # numeric
"mid=i" => \$mid, # numeric
"max=i" => \$mix, # numeric
"infile=s" => \$infile, # string
"outfile=s" => \$outfile, # string
"verbose" => \$verbose, # flag
) or die("Error in command line arguments\n");
环境变量
# In bash
FOO=123 BAR=456 ./perlScript.pl 789
# In perl
my ($foo) = $ENV{ FOO } || 0;
my ($bar) = $ENV{ BAR } || 0;
my ($baz) = $ARGV[0] || 0;
perldoc perlvar - 有关@ARGV 和%ENV 的详细信息
perldoc Getopt::Long