王朝网络
分享
 
 
 

Perl/TkFAQ-12.4变量必须申明为“my”吗?

王朝perl·作者佚名  2008-05-18
宽屏版  字体: |||超大  

原文:

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;

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有