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。