2017-05-22 73 views
-2

我正在嘗試Getopt::Long模塊讀取命令行參數,但由於某些原因,當我嘗試在打印語句中打印該變量時,它將打印「1」而不是已傳遞給該變量的值。如何使用Getopt :: Long檢索作爲選項值傳遞的確切值?

實施例:

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

的輸出是::

My name is 1 , My address is 1, My phone number is 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" 

使用以下命令運行上述代碼之後

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

閱讀的[ 「與值的選項」](http://perldoc.perl.org/Getopt/Long.html#Options-with-values)部手冊。總之:使用''name1 = s'=> \ $ name'等 – PerlDuck

+3

我熱烈地第二次[閱讀文檔]的建議(http://perldoc.perl.org/Getopt/Long.html#Options-with - 值)當一個模塊不工作時,你猜對了:-) –

回答

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::LongOptions with values

+0

它工作!謝謝。 –

相關問題