zhang jiaqiang wrote:
> 大家好,
> 
> 新手想不太明白一个变量作用的问题,请帮忙看看。
> 
> 我的问题是如何理解$x后两次没有值,为什么在函数in_a里还有值?而且第三次循 
> 环的时候这个值怎么成2了?

简单的解释是嵌套函数的原因。复杂的这里讲的很清楚,就不和你重复了。。

http://perl.apache.org/docs/general/perl_reference/perl_reference.html#my____Scoped_Variable_in_Nested_Subroutines

另外,没有特别需要,没有人会这么写 Perl 程序。

Qiang


> 
> 简化的程序如下:
> 
> for $i (0..2){
>     a();   
> }
> sub a
> {
>     my $x ;
>     in_a();
>     print $i,":\t",$x,"\n";
>     undef $x;
>     sub in_a
>     {
>       $x++;   
>       print "in_a:$x\n";
>     }   
> }
> 
> 执行结果:
> in_a:1
> 0:      1
> in_a:1
> 1:
> in_a:2
> 2:
> 
> 
> 
> Best regards
> Zhang JiaQiang
> 
> 
> > 


--~--~---------~--~----~------------~-------~--~----~
您收到此信息是由于您订阅了 Google 论坛“PerlChina Mongers 讨论组”论坛。
 要在此论坛发帖,请发电子邮件到 [email protected]
 要退订此论坛,请发邮件至 [email protected]
 更多选项,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问该论坛
-~----------~----~----~----~------~----~------~--~---

回复