Perl/TkFAQ-12.2与符号(&)是干什么用的?

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

原文:

12.2. What happened to the ampersands &?

Perl 4 programmers especially may be surprised to find that as of Perl 5.0 the ampersand & may be omitted in a call to a subroutine if the subroutine has been declared before being used. Actually you can even get around the declare before omit ampersand rule by using the subs.pm pragma, or by pre-declaring (without defining) as in a script like: #!/usr/bin/perl -w use strict; use Tk; sub Mysub; #pre-declare allows calling Mysub() ...Other main/Tk stuff - including call to Mysub() sans &... sub Mysub { ...Mysub stuff... }

Note however that one place the \& reference is sometimes used in perl/Tk in the setting up a callback for a widget. Other references are possible: e.g. \$foo is a reference to the scalar variable $foo (this was true even under perl 4).

译文:

12.2 与符号(&)是干什么用的?

从Perl5.0 以后,如果一个子程序预先申明过,那么在调用时可以省略前面的与符号(&)。这一点,可能会令很多人觉得奇怪,尤其是那些原来的Perl4程序员。实际上,我们甚至可以通过使用subs.pm的语法来把这个可以省略与符号的规则扩展开,或者通过在脚本中预申明(不是定义)来实现,例如:

#!/usr/bin/perl -w

use strict;

use Tk;

sub Mysub; #这里的预申明就允许下面直接使用Mysub()

……其它脚本部分,包括主程序和Tk的内容、对Mysub()的调用……

sub Mysub {

...Mysub stuff...

}

但是,请注意区别我们在Perl/Tk中为一个组件建立回应函数时所使用的\&reference,它是一个指向此子程序的引用。另外,还有一些其它的引用:例如\$foo是一个指向标量$foo的引用(这在Perl4中也可以用)。

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