Perl/TkFAQ-12.4变量必须申明为“my”吗?
来源:linux宝库作者:linux宝库 发布时间:2007-09-30 00:00:00


原文:
12.4. Must I use "my" on all my variables?
If you use strict; (as recommended) the answer is "probably". This confines the variables names to your namespace - so your variable does not conflict with one in the module(s) your are using (you are at the least useing Tk;). my does "lexical scoping" on a variable rather than the "dynamic scoping" done by local (like auto variables in C). The difference between these two is that the scope of my $var is confined to the block (sub, if, foreach, etc.) in which it is declared and used, as opposedto local $iable which can propogate to all blocks called by the block in which it is declared. In general the confined scope of my $var means that its use will proceed quicker and more efficiently than local $iable.
If you give a fully qualified variable name such as $main::var = 1; # No "my" needed
Then no my $var is needed. However, the lexical scoping of my $var makes it preferable.
If you choose to use my (as recommended) then beware that you should declare a variable my only at the first use (instantiation) of a variable. Consider yet another way to re-write the "Hello World!" script: #!/usr/local/bin/perl -w use strict; use Tk; my $main = new MainWindow; my $label = $main->Label(-text => ’Hello World!’); my $button = $main->Button(-text => ’Quit’, -command => sub{exit}); $label->pack; #no "my" necessary here $button->pack; #or here MainLoop;
Considering the finite number of names (in particular the high probability that a variable named $label or $button was used in one or more of the extensions to perl that you may be using) it helps one’s programming to use strict; and declare variables yours alone with my.
James M. Stern points out that redundant my declarations are not simply useless they can be dangerous as in the following script which will not work: #!/usr/local/bin/perl -w use strict; use Tk; my $main = new MainWindow; my $label = $main->Label(-text => ’Hello World!’); my $main; #WRONG: this $main overrides previous my $button = $main->Button(-text => ’Quit’, #will now fail -command => sub{exit}); $label->pack; $button->pack; MainLoop;
译文:
12.4 变量必须申明为“my”吗?
假如您使用了use strict;(推荐使用),答案是“也许应该”。因为这样就把您的变量名限定在您的脚本的名字空间中,这样您定义的变量就不会和您所使用的模块(您至少使用了Tk模块吧!)中的同名变量冲突了。my定义的变量是“词法范围”,而不同于local所定义的是“动态范围”(就像C语言中的自动变量?)。这两者之间的区分在于,my所定义的变量范围是他所在的程式块(例如,子程式,if,foreach等等);而local所定义的变量的范围则能够延伸到任何在其所定义的程式块中被调用的程式中去。一般说来,my $var所限制的变量范围意味着他比local $iable更快捷和高效。
假如您给出了完整的变量名,例如:
$main::var = 1; #无需“my”
那么就无需使用my $var。但是,my $var所定义的词法作用域使他具备更大的优势。
假如您选择使用my申明变量(建议大家使用),那么您必须注意只能在此变量第一次使用的时候(即初始化)对他进行申明。下面,我们考虑用另一种方法来重写“Hello World!”脚本:
#!/usr/local/bin/perl -w
use strict;
use Tk;
my $main = new MainWindow;
my $label = $main->Label(-text => ’Hello World!’);
my $button = $main->Button(-text => ’Quit’,
-command => sub{exit});
$label->pack; #no "my" necessary here
$button->pack; #or here
MainLoop;
试想,变量名字是很有限的(特别是那些极其常用的,如$label或$button等,他们可能同样会在一个或多个Perl的扩展模块中被使用),那么my申明能够帮助程式员能够使用use strict;同时申明自己的变量。
James M. Stern指出多余的my申明不但仅是无用的,而且会令您的脚本无法工作!例如:
#!/usr/local/bin/perl -w
use strict;
use Tk;
my $main = new MainWindow;
my $label = $main->Label(-text => ’Hello World!’);
my $main; #错误:这里的$main申明将覆盖前面的定义!
my $button = $main->Button(-text => ’Quit’, #现在将会失败!
-command => sub{exit});
$label->pack;
$button->pack;
MainLoop;
|
还没有关于此文章的相关评论!