perl 中的$a和$b

这两个变量是为sort函数准备的内置变量,所以声明的时候可以不加 my


use strict;
use warnings;

sub test {
    $a = 1;
    $b = 2;
    print $a, "\n";
    print $b, "\n";



from perldoc perlvar

perldoc perlvar

       $b      Special package variables when using sort(), see "sort" in
               perlfunc.  Because of this specialness $a and $b don't need to
               be declared (using use vars, or our()) even when using the
               "strict 'vars'" pragma.  Don't lexicalize them with "my $a" or
               "my $b" if you want to be able to use them in the sort()
               comparison block or function.


posted on 2012-04-19 10:44  perlman  阅读(991)  评论(0编辑  收藏  举报