哪些标识符被认为是动态的?
Which identifiers are considered to be dynamic?
以下摘录自perldoc perlmod
:
The package
statement declares the compilation unit as being in the given namespace. The scope of the package
declaration is from the declaration itself through the end of the enclosing block, eval
, or file, whichever comes first (the same scope as the my
() and local
() operators). Unqualified dynamic identifiers will be in this namespace, except for those few identifiers that if unqualified, default to the main package instead of the current one as described below.
上面 "Unqualified dynamic identifiers" 短语中的术语 "dynamic" 似乎指的是包中没有前缀 my
的变量。也就是说,在下面的代码片段中,$v1
被视为动态标识符。是吗?
package Package_1;
$v1 = "v1_val";
my $v2 = "v2_val";
变量范围的两种通用类型是动态和词法。基本上,词法变量的可见性基于它们在源代码中的位置,而动态变量的可见性在 运行 时间确定。
在 Perl 中,用 my
声明的变量是词法的,而任何其他变量都是动态的。这种区别变得直接相关的主要地方是 local
只能与动态(非 my
)变量一起使用,而不能与词法(my
)变量一起使用。
另请参阅 Perl 常见问题解答,What's the difference between dynamic and lexical (static) scoping?
查看差异的最佳方法是在代码中:
our $dynamic = 'outside';
my $static = 'outside';
sub show {
print "\tin sub: dynamic $dynamic\n";
print "\tin sub: static $static\n";
print"\n";
}
{
local $dynamic = 'inside';
my $static = 'inside';
print "In block\n";
print "\tinline: dynamic $dynamic\n";
print "\tinline: static $static\n";
show();
}
print "In main\n";
print "\tinline: dynamic $dynamic\n";
print "\tinline: static $static\n";
show();
输出:
In block
inline: dynamic inside
inline: static inside
in sub: dynamic inside
in sub: static outside
In main
inline: dynamic outside
inline: static outside
in sub: dynamic outside
in sub: static outside
注意子总是看到相同的(外部)$static
。
以下摘录自perldoc perlmod
:
The
package
statement declares the compilation unit as being in the given namespace. The scope of thepackage
declaration is from the declaration itself through the end of the enclosing block,eval
, or file, whichever comes first (the same scope as themy
() andlocal
() operators). Unqualified dynamic identifiers will be in this namespace, except for those few identifiers that if unqualified, default to the main package instead of the current one as described below.
上面 "Unqualified dynamic identifiers" 短语中的术语 "dynamic" 似乎指的是包中没有前缀 my
的变量。也就是说,在下面的代码片段中,$v1
被视为动态标识符。是吗?
package Package_1;
$v1 = "v1_val";
my $v2 = "v2_val";
变量范围的两种通用类型是动态和词法。基本上,词法变量的可见性基于它们在源代码中的位置,而动态变量的可见性在 运行 时间确定。
在 Perl 中,用 my
声明的变量是词法的,而任何其他变量都是动态的。这种区别变得直接相关的主要地方是 local
只能与动态(非 my
)变量一起使用,而不能与词法(my
)变量一起使用。
另请参阅 Perl 常见问题解答,What's the difference between dynamic and lexical (static) scoping?
查看差异的最佳方法是在代码中:
our $dynamic = 'outside';
my $static = 'outside';
sub show {
print "\tin sub: dynamic $dynamic\n";
print "\tin sub: static $static\n";
print"\n";
}
{
local $dynamic = 'inside';
my $static = 'inside';
print "In block\n";
print "\tinline: dynamic $dynamic\n";
print "\tinline: static $static\n";
show();
}
print "In main\n";
print "\tinline: dynamic $dynamic\n";
print "\tinline: static $static\n";
show();
输出:
In block
inline: dynamic inside
inline: static inside
in sub: dynamic inside
in sub: static outside
In main
inline: dynamic outside
inline: static outside
in sub: dynamic outside
in sub: static outside
注意子总是看到相同的(外部)$static
。