为什么Perl的foreach不需要用我的变量声明? [英] Why doesn't Perl's foreach require its variable to be declared with my?
问题描述
今天,我偶然发现了Perl中的一些东西,我并没有意识到:它本地化列表元素被分配给的变量。
当然,这是在Perl文档中记录的 - 但是我没有记住或读取它。以下脚本演示了我的意思:
使用警告;
:
使用strict;
my $ g = 99;
foreach $ g(1..5){
p($ g);
}
sub p {
my $ l = shift;
printf(%2d%2d \ n,$ g,$ l);
$ b脚本打印
99 1
99 2
99 3
99 4
99 5
因为
$ g
是本地化到foreach
loop。
据我所知,如果我已经将
my
添加到<$在foreach循环中,c $ c> $ g
$ p $ foreach $ g 5){
其实,我最终这样做是因为我觉得这个变量更清晰本地的循环。
我的问题是现在:是否有一个情况下,我使用我
确实有所作为(因为全局已经声明了 $ g
)。 调查行为记录在 perlsyn中的Foreach循环
forea ch
循环迭代正常的列表值,并将标量变量VAR设置为列表中的每个元素。如果变量前面有关键字my
,那么它在词汇上是有作用域的,因此只在循环中可见。
继续解释
否则,该变量对于循环隐式地是局部的并且在退出循环时重新获得其以前的值。如果变量之前是用
my
声明的,它使用该变量而不是全局变量,但是它仍然是本地化的。
因此,将它与 my
进行本地化或保留到 foreach
。
有点好奇的是
lockquote >
这个隐式的本地化只发生在 foreach
循环中。
foreach
循环默认以local
的方式动态地定位它的索引变量。但是,如果索引变量的前缀是关键字my
,或者如果在范围内已经有一个由该名称组成的词汇,则会创建一个新的词汇。
由于在这两种情况下创建了一个新的词法 ,因此不会有任何实际的区别。 我绝对支持并建议在那里放置 Today, I stumbled over something in Perl I was not aware of: it "localizes" the variable that the elements of the list iterated over is assigned to. This, of course, is documented in the Perl documentation - however I failed to remember or read it. The following script demonstrates what I mean: The script prints because As far as I can tell there is no difference if I had added Actually, I ended up doing it because I feel it makes it clearer that the variable is local to the loop. My question is now: is there a scenario where my using The investigated behavior is documented in Foreach Loops in perlsyn The which continues to the explanation Otherwise, the variable is implicitly local to the loop and regains its former value upon exiting the loop. If the variable was previously declared with Thus there should be no difference between localizing it with A little curiosity is that This implicit localization occurs only in a All this is further clarified in this snippet from Private Variables via The Since a new lexical is created inside in both cases there cannot be any practical difference. I absolutely support and recommend putting a 这篇关于为什么Perl的foreach不需要用我的变量声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ b my
。use warnings;
use strict;
my $g = 99;
foreach $g (1..5) {
p($g);
}
sub p {
my $l = shift;
printf ("%2d %2d\n", $g, $l);
}
99 1
99 2
99 3
99 4
99 5
$g
is "localized" to the foreach
loop.my
to $g
in the foreach loop:foreach my $g (1..5) {
my
does make a difference (given that $g
is already declared globally).
foreach
loop iterates over a normal list value and sets the scalar variable VAR to be each element of the list in turn. If the variable is preceded with the keyword my
, then it is lexically scoped, and is therefore visible only within the loop.
my
, it uses that variable instead of the global one, but it's still localized to the loop. my
or leaving that to foreach
.
foreach
loop.my()
from perlsub
foreach
loop defaults to scoping its index variable dynamically in the manner of local
. However, if the index variable is prefixed with the keyword my
, or if there is already a lexical by that name in scope, then a new lexical is created instead.my
there.