如何使用Getopt :: Long检索作为选项值传递的确切值?

时间:2017-05-22 11:54:11

标签: perl getopt-long

我正在尝试Getopt::Long模块来读取命令行参数,但出于某种原因,当我尝试在print语句中打印变量时,它会打印' 1'而不是传递给变量的值。

示例:

use Getopt::Long;
use warnings;
GetOptions(
                'name1' => \$name,
                'address' => \$add,
                'phone' => \$phone
        );
print "My name is $name , My address is $add, My phone number is $phone\n"

使用以下命令运行上述代码后:

perl getopt.pl --phone 77881100 --name1 Mart --address Ecity

输出结果为:

My name is 1 , My address is 1, My phone number is 1

我希望输出为:

My name is Mart , My address is Ecity, My phone number is 77881100

1 个答案:

答案 0 :(得分:2)

use warnings;
use strict;
use Getopt::Long;
GetOptions(
    'name1=s'   => \my $name,
    'address=s' => \my $add,
    'phone=s'   => \my $phone
);
print "My name is $name, My address is $add, My phone number is $phone\n"

请参阅Getopt::Long部分Options with values

部分