Perl中的符号 ->;、=>; 和 :: 分别表示什么意思?

 更新时间:2020年6月29日 13:42  点击:2327

What do the ->, => and :: symbols mean?

  The -> is the "infix dereference operator". In other words it is the means by which one calls a sub with a pass by reference (among other things you can do with ->). As stated above most things in calls to perl/Tk routines are passed by reference. The -> is used in perl just as in C or C++. (Most of the widget primitives are elements of the Tk:: "perl class".) A simple example of dereferencing would be: $x = { def => bar }; # $x is a reference to an anon. hash print $x->{def},"/n"; # prints ``bar''

  Note that in the case of calling perl/Tk subs there may be more than one way to call by reference. Compare my($top) = MainWindow->new;

  with my($top) = new MainWindow;

  But in general you will be making extensive use of calls like: $top -> Widge-type;

  There is a clear and succint discussion of references, dereferences, and even closures in man perlref(1) or see the perl 5 info page at: http://www.metronet.com/perlinfo/perl5.html

  The use of the => operator is quite common in perl/Tk scripts. Quoting from man perlop(1):

  The => digraph is simply a synonym for the comma operator. It's useful for documenting arguments that come in pairs.

  You could say that => is used for aesthetic or organizational reasons. Note in the following how hard it is to keep track of whether or not every -option has an argument: $query -> Button(-in,/$reply,-side,'left',-padx,2m,-pady, 2m,-ipadx,2m,-ipady,1m)->pack(-side,'bottom');

  As opposed to: $query ->Button( -in => /$reply, -side => 'left', -padx => 2m, -pady => 2m, -ipadx => 2m, -ipady => 1m )->pack(-side => 'bottom');

  By the way if you wanted the numeric "greater than or equal" you would use >= not =>.

  While the :: symbol can be thought of as similar to the period in a C struct, it is much more akin to the :: class scope operator in C++: a.b.c; /* something in C */ a::b::c(); // function in C++ $a::b::c; # a scalar in Perl 5 @a::b::c; # a list in Perl 5 %a::b::c; # an associative array or "hash" in Perl 5 &a::b::c; # a function in Perl 5

  It is also analogous to the single forward quotation mark in perl 4: $main'foo; # a $foo scalar in perl 4 $main::foo; # a $foo scalar in Perl 5

  For backward compatibility perl 5 allows you to refer to $main'foo but $main::foo is recommended.

  译文:

  符号->,=>和::分别表示什么意思?

  ‘- >'符号是“插入式解引用操作符”(infix dereference operator)。换句话说,它是调用由引用传递参数的子程序的方法(当然,还有其它的作用)。正如我们上面所提到的,在调用Perl/Tk的函数的时候,大部分参数都是通过引用传递的。Perl中的‘->'功能就和它们在C或C++中一样。(大部分原始的组件都是Tk中的Perl类的元素。)下面是一个简单的解引用的例子:

  $x = { def => bar }; # $x是指向一个匿名hash的引用

  print $x->{def},"/n"; # 输出``bar''

  注意,在调用Perl/Tk的子程序时有多种不同的方法进行引用。我们可以比较一下:

  my($top) = MainWindow->new;

  和

  my($top) = new MainWindow;

  两种方法的不同。

  但是,一般来说我们通常都使用这样的方法调用:

  $top -> Widge-type;

  在perlref的手册页中有详尽的关于引用、解引用、和闭包的讨论,或者也可以在下面的网页上查看Perl5的信息页:

  http://www.metronet.com/perlinfo/perl5.html

  在Perl/Tk的脚本中‘=>'操作符时很常见的。perlop手册页中说:关系操作符=>只是逗号操作符的替代物,它在显示成对的参数时非常有用。

  你可以认为=>只是为了程序的美观和易维护而被使用的。请看,在下面的例子中,要想监测是否每个选项都有对应的值,是多么的困难:

  $query -> Button(-in,/$reply,-side,'left',-padx,2m,-pady,

  2m,-ipadx,2m,-ipady,1m)->pack(-side,'bottom');

  而下面的这个则相反:

  $query ->Button( -in => /$reply,

  -side => 'left',

  -padx => 2m,

  -pady => 2m,

  -ipadx => 2m,

  -ipady => 1m

  )->pack(-side => 'bottom');

  顺便说一下,如果你需要用数字“大于等于”的符号,你应该用“>=”而不是“=>”。

  “::”符号可以认为是与C语言中的“.”相似的,而它更像C++中的::类范围操作符。

  a.b.c; /* C语言中的 */

  a::b::c(); // C++ 中的函数

  $a::b::c; # Perl 5中的标量

  @a::b::c; # Perl 5中的列表

  %a::b::c; # Perl 5中的关联数组(或叫hash)

  &a::b::c; # Perl 5中的函数

  另外,Perl4中的单撇号也具有相同的功能:

  $main'foo; # Perl 4中的标量$foo

  $main::foo; # Perl 5中的标量$foo

  出于向后兼容的考虑,Perl5也运行使用$main'foo,但是仍推荐使用$main::foo。

[!--infotagslink--]

相关文章

  • C# 中 “$” 符号的作用以及用法详解

    这篇文章主要介绍了C# 中 “$” 符号的作用以及用法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • Perl模块编写说明

    这两天在用Perl编写一些监控脚本,其实写代码也是一件挺有意思的事情,就是挺废时间的。而且,由于语法不太熟,基本想到一个东西都要先Google一下看怎么实现。...2020-06-29
  • Perl与JS的对比分析(数组、哈希)

    下面小编就为大家带来一篇Perl与JS的对比分析(数组、哈希)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-29
  • perl 模式匹配参数详解

    一、简介 模式指在字符串中寻找的特定序列的字符,由反斜线包含:/def/即模式def。其用法如结合函数split将字符串用某模式分成多个单词:@array = split(/ /, $line);二、匹配...2020-06-29
  • error LNK2019: 无法解析的外部符号 问题的解决办法

    error LNK2019: 无法解析的外部符号 问题的解决办法,需要的朋友可以参考一下...2020-04-25
  • Perl localtime时间函数的应用介绍

    Perl时间函数localtime的使用介绍,这里简单的介绍下,更多请查看官方介绍...2020-06-29
  • Perl中的特殊内置变量详细介绍

    这篇文章主要介绍了Perl中的特殊内置变量详细介绍,需要的朋友可以参考下...2020-06-29
  • 讲Perl中的本地时间与UNIX时间戳间相互转换的方法

    这篇文章主要介绍了讲Perl中的本地时间与UNIX时间戳间相互转换的方法,主要用到了Perl中的Date::Parse模块,需要的朋友可以参考下...2020-06-29
  • perl 列表和数组变量详解

    一、列表 列表是包含在括号里的一序列的值,可以为任何数值,也可为空,如:(1, 5.3 , "hello" , 2),空列表:()。 注:只含有一个数值的列表(如:(43.2) )与该数值本身(即:43.2 )是不同...2020-06-29
  • Perl学习笔记之CPAN使用介绍

    这篇文章主要介绍了Perl学习笔记之CPAN使用介绍,本文讲解了什么是CPAN、CPAN的目录作用介绍、CPAN安装Perl Module的两种方法等内容,需要的朋友可以参考下...2020-06-29
  • Perl 批量添加Copyright版权信息

    对所有输入文件,如果没有版权信息则加上版权信息,否则什么都不做,并对原文件以.bak结尾备份,需要的朋友可以参考下...2020-06-29
  • C# byte转为有符号整数实例

    这篇文章主要介绍了C# byte转为有符号整数实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-11-11
  • Perl中使用MIME::Lite发送邮件实例

    这篇文章主要介绍了Perl中使用MIME::Lite发送邮件实例,本文介绍了使用sendmail方式发送、发送HTML格式邮件、smtp方式发送邮件等内容,需要的朋友可以参考下...2020-06-29
  • Perl AnyEvent中的watcher实例

    这篇文章主要介绍了Perl AnyEvent中的watcher实例,关于AnyEvent请参阅的更多介绍请参阅文中的相关链接,需要的朋友可以参考下...2020-06-29
  • Java解析xml文件遇到特殊符号异常的情况(处理方案)

    这篇文章主要介绍了Java解析xml文件遇到特殊符号&会出现异常的解决方案,实现思路很简单通过在读取xml文件使用SAX解析前读取reader,具体实现方法及示例代码跟随小编一起看看吧...2021-05-14
  • perl获取日期与时间的实例代码

    perl获取日期与时间的例子,供大家学习参考...2020-06-29
  • php中 ->与 ==>符号的用法与区别

    本文章来给大家介绍一下在php中 ->与 ==>符号的用法与区别,有需要了解的朋友可尝试参考。 “->”(减号、右尖括号) 用于类中,访问类里的函数或对象,比如:...2016-11-25
  • Java中正则表达式split()特殊符号使用详解

    这篇文章主要介绍了Java中正则表达式split()特殊符号使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-07-21
  • [Php]去除魔术符号

    <?php /** * 去除魔术变换 */ class killMagicQuote { public function Strip_Magic_Slashes($varImp) { $varImp = is_array...2016-11-25
  • php中$美元符号与Zen Coding冲突问题解决方法分享

    Zen Coding插件就不多做介绍了。众所周知,安装了插件以后,输入$符号会被自动解析为相应的数字1、2、3...作为一名PHP程序员,想要通过其定义一些自己常用的代码。却发现展开以后悲剧的发现$符号全变成了数字。下面教你解...2014-05-31