欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-19 18:07:55
what do the ->, => and :: symbols mean?   the -> is the "infix dereferen...

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。

相关标签: Perl 符号