解释的不错。学习了。。。

在 2011年11月6日 下午8:21,woosley. xu. <[email protected]>写道:

> Getoptions函数需要把你的参数值传递给给$fileformat,所有要引用
>
>
> 比如
> #!/usr/bin/perl -w
> use warnings;
> my $a;
> sub changeValue($){
>     my $lo = shift;
>     $$lo = 5;
> }
>
> sub badChangeValue($){
>     my $a = shift;
>     $a = 5;
> }
>
> badChangeValue($a);
> print $a;
> changeValue($a);
> print $a;
> changeValue(\$a);
> print $a;
>
>
> woosley@woosley-PC ~
> $ perl t.pl
> Use of uninitialized value $a in print at t.pl line 15.
> Use of uninitialized value $a in print at t.pl line 17.
> 5
>
>
>
>
>
>
> 在 2011年11月6日 上午11:04,Dejian Zhao <[email protected]>写道:
>
>  Q2:好像明白了,是命令行参数引用了脚本内部的$Fileformat地址,谁引用谁都一样,操作的是同一块内存地址。
>>
>> perl -le 'my $a=1;my $b=\$a; $$b=5;print $a'
>> 输出结果是5,而不是1。
>> 命令行参数就相当于这里的$b,脚本内部的变量就相当于$a,给命令行参数传递值时就直接修改了脚本内部变量的值。
>>
>>
>> On 2011-11-6 10:56, Dejian Zhao wrote:
>>
>> Q1,Q3,Q4都解决了,Q2还是不太明白。
>>
>> Q2:将\$Fileformat改成$Fileformat是不可以的,会报错“Undefined argument in option
>> spec”,现在的问题是谁引用谁?我觉得应当是脚本内部的变量$Fileformat去引用命令行参数,应当是$Fileformat应当指向
>> 命令行参数fileformat的内存地址,但是从语法上讲,似乎是$Fileformat被引用,是命令行参数引用 了$Fileformat?
>> 难道谁引用谁都一样,实际是操作同一块内存地址?
>>
>>
>> Q3: 原来"fileformat:s"=>\$Fileformat可以写成"fileformat:s"=>\$fileformat
>> (当然前提是把my $Fileformat改为my
>> $fileformat),我看到的脚本中都是左侧用小写,右侧把第一个字母大写,就产生了误解。可能是个人习惯的问题吧,我更喜欢写
>> 成"fileformat:s"=>\$fileformat。
>>
>> Q4: 测试了,是这样子的。其余参数保存进了@ARGV
>>
>> On 2011-11-6 6:47, Michael Zeng wrote:
>>
>> perldoc GetOpt::Long 应该能看懂啊,不过manual很多,我记得
>>
>> 我来回答下
>>
>> 1、我查看了perldoc Getopt::Long,大体知道 fileformat是在命令行中使用的参数,应当
>> 是一个string,然后传递到程序内部时由$Fileformat接收这个值。是这样么?有没有误读?
>> 左边是命令行要出现的东西, 右边是自己输入的参数,比如 这个脚本用做
>> test.pl -fileformat  AAA -cutoff BBB -verbose CCCC -help DDD ,
>> 命令行输入的AAA, BBB, CCC , DDD 自动的被替换成那 4 个变量, 从而被获取,
>>
>>
>> 2、但是为什么是\$Fileformat,一个reference呢?写成"fileformat:s"=&
>> gt;$Fileformat可不可以,为什么?二 者什么区别?
>> 是用引用,一般都是引用,是一个指针的东西,
>>
>>
>> 3、另外,为什么参数开关使用了fileformat,在内部就不能使用$fileformat,而是换 成$Fileformat 呢? 在
>> test.pl内 部并没有占用$fileformat这个变量啊?
>> 脚本一开始应该定义 了 my ($Fileformat,$Cutoff,$Verbose,$Help); 了,当然是只能用这些变量,
>>
>>
>> 4、在命令行中运行此程序时,带开关的参数传到程序内部由GetOptions来处理,没有被参数开关指定的参数都保存进 @ARGV?
>> 这个不记得,应该是,
>>
>>
>> 再多看看 manual,
>>
>>
>>
>>
>> 2011/11/6 Dejian Zhao <[email protected]>
>>
>>> 下面代码是在别人脚本(test.pl)中看到的,有几个问题,希望达人给解答一下。 多 谢!
>>>
>>> ==========test.pl========
>>> use strict;
>>> use Getopt::Long;
>>>
>>> my ($Fileformat,$Cutoff,$Verbose,$Help);
>>> GetOptions(
>>>     "fileformat:s"=>\$Fileformat,
>>>     "cutoff:s"=>\$Cutoff,
>>>     "verbose"=>\$Verbose,
>>>     "help"=>\$Help
>>> );
>>> die `pod2text $0` if (@ARGV == 0 || $Help);
>>> =========================
>>>
>>> 主要是GetOptions()里面的部分看 不懂,以 "fileformat:s"=>\$Fileformat为例来解读这段代码。
>>>
>>> 1、我查看了perldoc Getopt::Long,大体知道 fileformat是在命令 行中 使用的参数,应当 是一个string
>>> ,然后传递到程序内部时由$Fileformat接 收这个值。是这样么?有没有误读?
>>>
>>> 2、但是为什么是\$Fileformat,一个reference呢?写成"fileformat:s"=>$Fileformat可 不
>>> 可以,为什么?二 者什么区别?
>>>
>>>
>>> 3、另外,为什么参数开关使用了fileformat,在内部就不能使用$fileformat, 而是换成$Fileformat呢? 在
>>> test.pl内 部并没有占用$fileformat这个变量啊?
>>>
>>> 4、在命令行中运行此程序时,带开关的参数传到程序内部由GetOptions来处理,没有被参数开关指定的参数都保存进@ARGV?
>>>
>>
>>
>>  --
>> 您收到此邮件是因为您订阅了 Google 网上论坛的“PerlChina Mongers 讨论组”论坛。
>> 要向此网上论坛发帖,请发送电子邮件至 [email protected]。
>> 要取消订阅此网上论坛,请发送电子邮件至 [email protected]。
>> 若有更多问题,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问此网上论坛。
>>
>
>
>
> --
> woosley.xu.    http://twitter.com/redicaps
>
>
>
>
>  --
> 您收到此邮件是因为您订阅了 Google 网上论坛的“PerlChina Mongers 讨论组”论坛。
> 要向此网上论坛发帖,请发送电子邮件至 [email protected]。
> 要取消订阅此网上论坛,请发送电子邮件至 [email protected]。
> 若有更多问题,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问此网上论坛。
>



-- 
Thanks!

O(∩_∩)O
--Try my best to do everything!--

-- 
您收到此邮件是因为您订阅了 Google 网上论坛的“PerlChina Mongers 讨论组”论坛。
要向此网上论坛发帖,请发送电子邮件至 [email protected]。
要取消订阅此网上论坛,请发送电子邮件至 [email protected]。
若有更多问题,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问此网上论坛。

回复