| 導購 | 订阅 | 在线投稿
分享
 
 
 

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

2008-05-18 21:55:26  編輯來源:互聯網  简体版  手機版  評論  字體: ||
 
 
  原文:

  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)
 
 
 
  原文:   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)
󰈣󰈤
日版宠物情人插曲《Winding Road》歌词

日版宠物情人2017的插曲,很带节奏感,日语的,女生唱的。 最后听见是在第8集的时候女主手割伤了,然后男主用嘴帮她吸了一下,插曲就出来了。 歌手:Def...

兄弟共妻,我成了他们夜里的美食

老钟家的两个儿子很特别,就是跟其他的人不太一样,魔一般的执着。兄弟俩都到了要结婚的年龄了,不管自家老爹怎么磨破嘴皮子,兄弟俩说不娶就不娶,老父母为兄弟两操碎了心...

 
 
>>返回首頁<<
 
 
 
 
 熱帖排行
 
 
王朝网络微信公众号
微信扫码关注本站公众号 wangchaonetcn
 
  免责声明:本文仅代表作者个人观点,与王朝网络无关。王朝网络登载此文出于传递更多信息之目的,并不意味著赞同其观点或证实其描述,其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有