为什么 Perl(5.30.3 但也包括
no feature ':all'
)假定连接运算符 .
后面的裸字是字符串?例如:use v5.30; # or: no feature ':all';
use strict;
use warnings;
my $k1 = 'a';
my $k2 = 'b';
print STDOUT $k1 . k2 . "\n";
输出结果为ak2
(预期的是ab
),并且没有引发关于不允许此处使用裸字的异常。虽然这是我的脚本中的错误,但不一定是Perl的问题。
根据perlglossary
的说法,裸字是“足够模糊以被认为在use strict 'subs'
下是非法的单词”。然而,由于在k2
之前定义了一个名为$k2
的标量变量,我本以为k2
已经足够模糊。
奇怪的是,第二个裸字会引发一个异常:
use v5.30; # or: no feature ':all';
use strict;
use warnings;
my $k1 = 'a';
my $k2 = 'b';
my $k3 = 'c';
print STDOUT $k1 . k2 . k3 . "\n";
并且:
Bareword "k3" not allowed while "strict subs" in use at mwe.pl line 7.
Execution of mwe.pl aborted due to compilation errors.
k3
是不被允许的,但k2
是可以的。为什么?