Perl中foreach循环的默认范围是多少?
在Perl中,在foreach循环中使用’my’有什么作用吗?似乎索引变量始终是本地的,不管是否使用’my’.那么你可以在foreach循环中放置’我的’,并且在循环体内仍然有私有的范围?
可以看出,使用’for’循环使用/不使用’my’有区别: use strict; use warnings; my ($x,$y) = ('INIT','INIT'); my $temp = 0; for ($x = 1; $x < 10; $x++) { $temp = $x+1; } print "This is x: $xn"; # prints 'This is x: 10'. for (my $y = 1; $y < 10; $y++) { $temp = $y+1; } print "This is y: $yn"; # prints 'This is y: INIT'. 但是在foreach它似乎没有效果: my ($i,$j) = ('INIT','INIT'); foreach $i (1..10){ $temp = $i+1; } print "nThis is i: $in"; # prints 'This is i: INIT'. foreach my $j (1..10){ $temp = $j+1; } print "nThis is j: $jn"; # prints 'This is j: INIT'. 解决方法
从
http://perldoc.perl.org/perlsyn.html#Foreach-Loops:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |