王朝网络
分享
 
 
 

和::分别表示什么意思?

王朝other·作者佚名  2008-05-18
宽屏版  字体: |||超大  

原文:

12.1. 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.

译文:

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

‘- >’符号是“插入式解引用操作符”(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。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有