王朝网络
分享
 
 
 

Perl/TkFAQ-8.如何写Perl/Tk脚本

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

原文:

8. How do I write scripts in perl/Tk?

Start your script as you would any perl script (e.g. #!/usr/bin/perl, #!/usr/local/bin/perl, #!/opt/bin/perl, [built static? then #!/usr/bin/tkperl], whatever, see the perlrun(1) man page for more information).

Throwing the -w warning switch is recommended.

The use of the statement use strict; is recommended.

Use of the statement use Tk; is required.

A simple "Hello World!" widget script could be written as follows: #!/usr/local/bin/perl -w use strict; use Tk; my $main = new MainWindow; $main->Label(-text => 'Hello World!' )->pack; $main->Button(-text => 'Quit', -command => sub{exit} )->pack; MainLoop;

The MainLoop; statement is the main widget event handler loop and is usually found in perl/Tk scripts (usually near the end of the main procedure after the widgets have been declared and packed). MainLoop; is actually a function call and you may see it written as MainLoop();, &Tk::MainLoop;, &Tk::MainLoop();, etc.

Note the use of the -> infix dereference operator. Most things in calls to perl/Tk routines are passed by reference.

Note also the use of the => operator which is simply a synonym for the comma operator (well it is a bit more than that :-). In other words, the arguments that get passed to Label and Button in the above example are good old perl associative arrays (perl 5 people prefer to call them "hashes" however). Indeed, we might have written the above as: #!/usr/local/bin/perl -w use strict; use Tk; my $main = new MainWindow; $main->Label(-text , 'Hello World!' )->pack; $main->Button(-text , 'Quit', -command , sub{exit} )->pack; MainLoop;

Or even as: #!/usr/local/bin/perl -w use strict; use Tk; my $main = new MainWindow; my %hello = ('-text','Hello World!'); my %quit_com = ('-text' => 'Quit', '-command' => sub{exit}); $main->Label(%hello)->pack; $main->Button(%quit_com)->pack; MainLoop;

Note however, that the use of the => in the first method of writing this script makes it look more "Tcl-ish" :-).

Lastly, we note the extensive use of the my function in most perl/Tk programs. my is roughly equivalent to local in Perl 4 - but is purported to be "faster and safer" as well as much more strictly local in scope. See perlfunc(1) manpage for more information on my.

Other examples of code may be found in the perl5/Tk/demos/ directory and in perl5/Tk/demos/widget_lib/.

(A variant on this scipt called hello is available in the file perl5/Tk/demos/hello in your own pTk distribution. Also, 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 filename, edit the first line to point to your perl interpreter, then: chmod u+x filename, then execute: filename.)

译文:

8. 如何写Perl/Tk脚本?

脚本的首行应该是指明Perl解释器的位置,例如#!/usr/bin/perl,#!/usr/local/bin/perl,#! /opt/bin/perl等等(如果是静态编译的,那么就要用#!/usr/bin/tkperl)。要了解更详细的信息,请参阅手册页:

man 1 perlrun

对于初学者而言,最好用-w开关打开警告信息,同时,推荐你使用use strict;语句。当然,作为Tk程序,语句use Tk;是必不可少的。

下面是一个简单的“Hello World!”窗口脚本程序:

#!/usr/local/bin/perl -w

use strict;

use Tk;

my $main = new MainWindow;

$main->Label(-text => 'Hello World!'

)->pack;

$main->Button(-text => 'Quit',

-command => sub{exit}

)->pack;

MainLoop;

这里,MainLoop;语句就负责循环的处理主窗口中的各组件所触发的事件,一般Perl/Tk的脚本中都有这个语句(通常是在主程序的末尾,即各自组件声明之后)。MainLoop;实际上是一个函数调用,所以它也可以被写成如下的形式:

MainLoop();

&Tk::MainLoop;

&Tk::MainLoop();

另外,请注意这里“->”引用操作符的使用,很多Perl/Tk的函数都是使用的引用来调用的。

同时,这里的“=>”操作符其实就是逗号操作符的的另一种写法(当然,可能会用处更多一些:P)。换句话说,在上面的例子中,我们传递给Label和Button函数的参数其实就是Perl的关联数组(associative array)(当然,Perl5中我们一般称之为hash)。因此,实际上我们也可以把上面的例程写成:

#!/usr/local/bin/perl -w

use strict;

use Tk;

my $main = new MainWindow;

$main->Label(-text , 'Hello World!'

)->pack;

$main->Button(-text , 'Quit',

-command , sub{exit}

)->pack;

MainLoop;

或者,甚至是这样:

#!/usr/local/bin/perl -w

use strict;

use Tk;

my $main = new MainWindow;

my %hello = ('-text','Hello World!');

my %quit_com = ('-text' => 'Quit', '-command' => sub{exit});

$main->Label(%hello)->pack;

$main->Button(%quit_com)->pack;

MainLoop;

但是,请注意,最初的例子中的使用“=>”的写法可能使你的脚本更加像Tcl。

最后,我们会发现Perl/Tk的程序中很广泛的使用“my” 声明。在Perl4里my基本上等同于local,但是据说除了更加严格的范围定义外,还会更快和安全。(译者注:在Perl5中my和local是完全不同的概念!可以用perldoc –f my和perldoc –f local来查阅手册——windows也可以哟!)

其它的例程,你可以在perl5/Tk/demos/和perl5/Tk/demos/widget_lib/目录中找到。

(另外,在你的pTk的安装包中的demos/目录中有一个名为hello的脚本,实际上就和上面的例子很相似。要使用网页上的代码,你首先要把它保存为一个本地的文件如filename,然后修改它的第一行为你的系统中的Perl解释器的位置,最后:chmod u+x filename,这样就可以直接运行filename了。——译者注:如果想省事,其实可以存下来以后就直接使用:perl filename来运行!:-P)

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