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

即使打开了strict和warnings选项也无妨,下面代码并无错误和警告。

use strict;
use warnings;

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

test();

1;

from perldoc perlvar

perldoc perlvar

       $a
       $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.

==

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-15
  • 2021-07-19
  • 2021-12-13
猜你喜欢
  • 2021-08-24
  • 2022-12-23
  • 2022-12-23
  • 2021-11-10
  • 2022-12-23
  • 2022-02-03
  • 2022-12-23
相关资源
相似解决方案