gpt4 book ai didi

perl - 使用严格;在 Perl wrt $a 和 $b 中,行为未按预期工作

转载 作者:行者123 更新时间:2023-12-04 00:43:31 25 4
gpt4 key购买 nike

我写了一个示例 perl 代码:

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

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

最佳答案

$a$b 是特殊变量,因此与 strict 一起使用时不会出现错误。

来自 perldoc strict ,

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

关于perl - 使用严格;在 Perl wrt $a 和 $b 中,行为未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24646689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com