王朝网络
分享
 
 
 

Perl/TkFAQ-12.基本问题

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

原文:

12. Common Problems.

Everything in Tk-land is a reference. When defining callbacks take care to pass variables by reference. Callbacks are closures and to ensure a variable gets its current value, as opposed to its value when the callback is defined, pass by reference, e.g.: $frog = 123; $b = $mw->Button( -text => 'Push Me', -command => [ sub { my($widget, $frog) = @ARG; print STDERR "widget=$widget!\n"; print STDERR "frog=$$frog!\n"; }, $mw, \$frog, ], ); # end Button definition

If $frog is not passed by reference the print statement will always output "123" (actually, the print as it exists will print nothing since it's trying to dereference $frog, which presumably is now not a reference). Note that by definition all perl/Tk widgets are already references, since they're simply Perl objects, and that's why you do not have to print $$widget!

A good "reference" for handling references and dereferencing are the perlref(1) and perlobj(1) man pages. A good "reference" for the various data types you will encounter in this kind of perl programming is Tom Christiansen's Perl Data Structures Cookbook which is now available as the perldsc(1) man page.

Also beware the traps that befall perl4 programmers in making the move to perl 5. References for this include the new perltrap(1) man page as well as William Middleton's perl425 trap document at: http://www.perl.com/perl/all_about/perl425.htmlor http://www.perltk.org/contrib/misc/perl425.html

译文:

12. 基本问题

Tk范围内的所有组件都是引用(reference)。在定义回应函数的时候要注意通过引用来传递变量。因为回应函数是关闭的,所以要想保证一个传人的变量得到的是它的当前值,而不是在回应函数定义时的值,就必须通过引用来传递。例如:

$frog = 123;

$b = $mw->Button(

-text => 'Push Me',

-command => [

sub {

my($widget, $frog) = @ARG;

print STDERR "widget=$widget!\n";

print STDERR "frog=$$frog!\n";

}, $mw, \$frog,

],

); # end Button definition

这里,如果$frog不是通过引用传递的,那么print语句会总是输出”123”(实际上,这个print语句不会输出任何信息,因为它总是试图解开$ frog的引用,而其实它根本不是……)。注意,所有的Perl/Tk组件在定义的时候就已经是引用了,因为它们也都只是Perl的对象,这也就是为什么我们不需要print $$widget的原因。

要掌握引用和解引用的最后的参考资料就是perlref和perlobj的手册页。而对于我们将在这一类型的Perl编程中会遇到的各种各样的数据类型,Tom Christiansen的《Perl Data Structures Cookbook》将是很好的参考资料,而且它现在已经在perldsc的手册页中了!

同时也要注意在perl4到perl5过程中编程上可能出现的陷阱。关于这个问题的参考资料包括新的perltrap手册页和William Middleton的关于perl425陷阱的文档:

http://www.perl.com/perl/all_about/perl425.html

http://www.perltk.org/contrib/misc/perl425.html

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
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- 王朝网络 版权所有