Username: Password:

Perl/TkFAQ-12.2和符号(&)是干什么用的?
来源:linux宝库作者:linux宝库 发布时间:2007-09-30 00:00:00


  原文:

  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中也能够用)。

喜欢本文,那就收藏到:

    Del.icio.us Google书签 Digg Live Bookmark Technorati Furl Yahoo书签 Facebook 百度搜藏 新浪ViVi 365Key网摘 天极网摘 和讯网摘 博拉网 POCO网摘 添加到饭否 QQ书签 Digbuzz我挖网
相关评论  我也要评论
还没有关于此文章的相关评论!
  • 昵称: (为空则显示guest)
  • 评论分数: ★ ★ ★★★ ★★★★ ★★★★★
  • 评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
  • 导航
    赞助商
    文章类别
    订阅