当前位置:资讯中心主页 >Perl >文章内容
- Perl/TkFAQ-10.1.怎么使用按钮来调用Perl的子程式
- 来源:linux宝库作者:linux宝库 发布时间:2007-09-30 08:00:00
-
- 域名惊喜价格 cn域名1元注册
- com域名39.9元
域名注册
- 主机按月支付,低至19元/月
- 超大流量,可开子站点
虚拟主机
- 特惠VPS168元/月,4-8M独享带宽保证
- 独立操作系统,无限开站点
VPS主机
原文:
10.1. How do I get a Button to call a Perl subroutine?
You may specify the -command option in the call to create & pack the button as in: $main->Button(-text => ’Print’, -command => sub{do_print($filename, $font)} )->pack;
Where sub do_print { } is a subroutine that handles two arguments and is declared elsewhere in the script. A full script example of the use of the above code is presented in the second example(s) in UserGuide.pod
(Full source code for this and other examples from UserGuide.pod may be found at http://www.perltk.org/contrib/pod/. To load code from the web save as a local file say ex1.pl, edit the first line to point to your perl interpreter, then change permission: %chmod u+x ex1.pl, then execute the script: %ex1.pl.)
The above method is called the "anonymous subroutine (closure)" method. As discussed in Callback.pod one might have re-written that statement to use the "reference to a sub" method thusly: $main->Button(-text => ’Print’, -command => [ \&do_print , $filename, $font ] )->pack;
Note the backslash in front of \&do_print. This causes perl to generate a reference to sub do_print rather than call it. (thanks Jim Stern :-)
译文:
10.1. 怎么使用按钮来调用Perl的子程式?
在创建和布置按钮的时候,能通过设定-command选项来指定某个子程式,例如:
$main->Button(-text => ’Print’,
-command => sub{do_print($filename, $font)}
)->pack;
其中,子程式do_print{}是在脚本的其他地方定义的,他能接受两个参数。UserGuide.pod中的第二个例子就是使用上面的代码的完整的样本。
(译者注:由于原文中提供的链接无法使用,建议大家直接在自己安装的Tk目录中找就能了。)
在上面的例子中使用了“匿名子程式”(anonymous subroutine)方法。根据Callback.pod中的说明,我们能用“子程式引用”的方法来改写上面的语句,如下:
$main->Button(-text => ’Print’,
-command => [ \&do_print , $filename, $font ]
)->pack;
请注意,在\&do_print之前的反斜杠(\)。这种用法能使Perl不是调用这个子程式,而是产生出一个指向子程式do_print的引用。(感谢Jim Stem)
以上内容由 华夏名网 搜集整理,如转载请注明原文出处,并保留这一部分内容。
“华夏名网” http://www.sudu.cn 和 http://www.bigwww.com 是成都飞数科技有限公司的网络服务品牌,专业经营虚拟主机,域名注册,VPS,服务器租用业务。公司创建于2002年,经过6年的高速发展,“华夏名网”已经成为我国一家知名的互联网服务提供商,被国外权威机构webhosting.info评价为25大IDC服务商之一。 - (阅读次数:75)
- 上一篇: Perl/TkFAQ-10.2.如何设置按钮随鼠标指向的动态变化 下一篇: Perl/TkFAQ-10.如何使用某个组件来完成某个工作
- [
收藏] [
推荐] [
评论] [打印本页] [返回上一页][关闭窗口]


