如何将命令行参数传递给Perl程序?

时间:2022-03-08 00:12:07

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

我正在开发一个Perl脚本。如何将命令行参数传递给它?

Example:

例子:

script.pl "string1" "string2"

9 个解决方案

#1


175  

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:

如果它们有不同的含义,您可以使用GetOpt: Std和GetOpt: Long来轻松地处理它们。Std只支持单字符开关,而GetOpt::Long更灵活。从GetOpt::长:

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是一个特殊的变量,它包含所有命令行参数。第一个是$ARGV[0]。“string1”在你的情况下)和$ARGV[1]是第二个参数。您不需要一个特殊的模块来访问@ARGV。

#2


58  

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

就像你想的那样,你把它们传递进去,在你的脚本中,你从@ARGV数组中获取它们。像这样:

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


29  

foreach my $arg (@ARGV) {
    print $arg, "\n";
}

will print each argument.

将打印每个参数。

#4


24  

Alternatively, a sexier perlish way.....

或者,更性感的方式……

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

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

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

假设传递了两个值。额外的代码可以验证这个假设是安全的。

#5


19  

Yet another options is to use perl -s, eg:

另一个选择是使用perl -s,例如:

#!/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:

或参阅原文了解更多详情:

#6


13  

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

通过将特殊变量@ARGV分配给变量列表,您可以直接访问它们。举个例子:

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

perl tmp.pl 1 2 3 4 5

perl tmp。1 2 3 4 5

如何将命令行参数传递给Perl程序?

#7


7  

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

如果参数是要读取的文件名,请使用diamond(<>)操作符来获取其内容:

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.

如果参数是选项/开关,使用GetOpt: Std或GetOpt::Long,如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:

如果它们是别的东西,那么您可以通过@ARGV显式访问它们,或者使用shift命令:

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

(注意,只有在所有潜艇不在的情况下,使用shift操作才有效。在子程序中,它将检索传递给子程序的参数列表,而不是传递给程序的参数列表。

#8


5  

my $output_file;

if((scalar (@ARGV) == 2) && ($ARGV[0] eq "-i"))

{

$output_file= chomp($ARGV[1]) ;


}

#9


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.

如果只想要一些值,可以使用@ARGV数组。但是,如果您想要寻找更强大的命令行选项处理,您应该使用Getopt::Long。

#1


175  

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:

如果它们有不同的含义,您可以使用GetOpt: Std和GetOpt: Long来轻松地处理它们。Std只支持单字符开关,而GetOpt::Long更灵活。从GetOpt::长:

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是一个特殊的变量,它包含所有命令行参数。第一个是$ARGV[0]。“string1”在你的情况下)和$ARGV[1]是第二个参数。您不需要一个特殊的模块来访问@ARGV。

#2


58  

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

就像你想的那样,你把它们传递进去,在你的脚本中,你从@ARGV数组中获取它们。像这样:

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


29  

foreach my $arg (@ARGV) {
    print $arg, "\n";
}

will print each argument.

将打印每个参数。

#4


24  

Alternatively, a sexier perlish way.....

或者,更性感的方式……

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

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

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

假设传递了两个值。额外的代码可以验证这个假设是安全的。

#5


19  

Yet another options is to use perl -s, eg:

另一个选择是使用perl -s,例如:

#!/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:

或参阅原文了解更多详情:

#6


13  

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

通过将特殊变量@ARGV分配给变量列表,您可以直接访问它们。举个例子:

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

perl tmp.pl 1 2 3 4 5

perl tmp。1 2 3 4 5

如何将命令行参数传递给Perl程序?

#7


7  

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

如果参数是要读取的文件名,请使用diamond(<>)操作符来获取其内容:

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.

如果参数是选项/开关,使用GetOpt: Std或GetOpt::Long,如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:

如果它们是别的东西,那么您可以通过@ARGV显式访问它们,或者使用shift命令:

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

(注意,只有在所有潜艇不在的情况下,使用shift操作才有效。在子程序中,它将检索传递给子程序的参数列表,而不是传递给程序的参数列表。

#8


5  

my $output_file;

if((scalar (@ARGV) == 2) && ($ARGV[0] eq "-i"))

{

$output_file= chomp($ARGV[1]) ;


}

#9


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.

如果只想要一些值,可以使用@ARGV数组。但是,如果您想要寻找更强大的命令行选项处理,您应该使用Getopt::Long。