使用严格;Perl wrt $a 和 $b 中的行为未按预期工作 [英] use strict; behaviour not working as expected in Perl wrt $a and $b

查看:44
本文介绍了使用严格;Perl wrt $a 和 $b 中的行为未按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个示例 perl 代码:

I have written a sample perl code:

use strict;
use warnings;
$a=1;
$b=2;
if($b==2) {
    $a=3;
}
print $a;

理想情况下,当我运行此代码时,它应该给出一个错误,因为全局符号$a"需要明确的包名称..."但它没有给出任何错误.它给出的输出为3".为什么这样?据我所知,如果我们使用strict,那么我们需要定义变量的范围,否则会报错.我的理解有误吗?

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?

推荐答案

$a$b 是特殊变量,因此与 <一起使用时不会出现上升错误代码>严格.

$a and $b are special variables, and thus are not rising error when used with strict.

来自 perldoc strict,

由于 sort() 的特殊用途,变量 $a 和 $b 免除此检查.

Because of their special use by sort(), the variables $a and $b are exempted from this check.

这篇关于使用严格;Perl wrt $a 和 $b 中的行为未按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆