Perl中的符号 ->;、=>; 和 :: 分别表示什么意思?
作者:mdxy-dxy 发布时间:2022-11-28 14:38:48
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。


猜你喜欢
- 一、watch使用的几种方法1、 通过 watch 监听 msg 数据的变化。watch: { msg(oldVa
- 如何在生产上部署Django?Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比较常见的一种方式。uwsgi介绍uW
- 如何在VSCode中添加Python解释器并安装Python库一. 安装VScode编辑器双击“VSCodeUserSe
- /*Bresenham画圆算法*/var arc = function(x0,y0,r){/*起点坐标x0,y
- Python 界有条不成文的准则: 计算密集型任务适合多进程,IO 密集型任务适合多线程。本篇来作个比较。通常来说多线程相对于多进程有优势,
- 这次,我们再来用Ursina引擎来做一个太阳系行星模拟器吧!想要了解Ursina 3D引擎的基本使用方法的话,查看我的另一篇文章:详解Pyt
- Postgres如何存储文件postgres提供了两种不同的方式存储二进制,要么是使用bytea类型直接存储二进制,要么就是使用postgr
- arguments定义所有的函数都有一个自己的arguments对象,用来储存它实际接受到的参数,而不局限于函数声明时所定义的参数列表。它不
- keras中的Reshapekeras自带from keras.layers import Reshapelayer_1 = Reshape
- 在Pydev能正常执行的脚本,在导出后在命令行执行,通常会报自己写的包导入时找不到。一:报错原因在PyDev中,test.py 中导入Tes
- 原来在robotframework中使用press key方法进行键盘的操作,但是该方法需要写 * 作对象的locator,不是很方便,现在找
- 在python中调用fortran代码,要用到f2py这个程序。它的项目主页在此。现在该项目已经合并到numpy中了,先安装python再装
- 进程和线程的区别和联系终于开始加深难度,来到进程和线程的知识点~单就这两个概念,就难倒过不少初学者—&mdash
- 相信大家平时都有这样的经历:页面上有一个链接指向服务器一个Word文件,当客户端机器有安装Office时,点击链接将调用Word打开浏览;当
- PHP从主流来看,是一门面向过程的语言,它的最大缺点就是无法实现多线程管理,其程序的执行都是从头到尾,按照逻辑一路执行下来,不可能出现分支,
- 想到一个好玩的,运行如下 javascript :if ('0') alert("'0' is t
- 1.阈值化分割原理通过对图像的灰度直方图进行数学统计,选择一个或多个阈值将像素划分为若干类。一般情况下,当图像由灰度值相差较大的目标和背景组
- 关于什么是并发模型,我在这里引用 Go 语言联合创造者 Rob Pike 的一段话:并发是指一次处理多件事。并行是指一次做多件事。二者不同,
- 1. 用途(?(id/name)yes-pattern|no-pattern)的作用是:对于给出的id或者name,先尝试去匹配
- 本文实例讲述了Python实现Sqlite将字段当做索引进行查询的方法。分享给大家供大家参考,具体如下:默认从sqlite中获取到的数据是数