use strict; behaviour not working as expected in Perl wrt $a and $b

最后都变了- 提交于 2020-12-26 07:55:03

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!