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

时间:2021-05-22

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:///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;

  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。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章