问题
I have written a sample perl code:
use strict;
use warnings;
$a=1;
$b=2;
if($b==2) {
$a=3;
}
print $a;
Ideally, when I run this code, it should give an error as 'Global symbol "$a" requires explicit package name...' But it is not giving any error. It gives the output as '3'. Why so? As far as I know, if we use strict, then we need to define the scope of the variable otherwise it gives an error. Is my understanding wrong?
回答1:
$a
and $b
are special variables, and thus are not rising error when used with strict
.
From perldoc strict,
Because of their special use by sort(), the variables $a and $b are exempted from this check.
来源:https://stackoverflow.com/questions/24646689/use-strict-behaviour-not-working-as-expected-in-perl-wrt-a-and-b