【发布时间】:2011-03-09 15:46:49
【问题描述】:
全局符号需要明确的包名?为什么会发生这种情况以及可能导致此错误的各种情况是什么?
【问题讨论】:
-
示例 perl 代码?请参阅stackoverflow.com/q/4257179/10468 或搜索有关 perl 模块的问题。另请参阅(异地)sitepoint.com/forums/…
标签: perl
全局符号需要明确的包名?为什么会发生这种情况以及可能导致此错误的各种情况是什么?
【问题讨论】:
标签: perl
也可能发生在前面的语句不完整的情况下。
use strict;
sub test;
test()
# some comment
my $x;
perl 现在抱怨以下错误消息:
my "
Global symbol "$x" requires explicit package name
错误不在“my”的声明中,而是在test() 处缺少的分号 (;)。
【讨论】:
看看perldiag:
全局符号“%s”需要明确的包名
(F) 你说过“use strict”或“use strict vars”,这表示所有变量必须要么是词法范围的(使用“my”或“state”),要么事先使用“our”声明,或者明确限定全局变量在哪个包中(使用“::”)。
【讨论】:
为了在您的代码中明确说明导致它的原因,您需要发布您的代码。
错误是输出,您的脚本已停止,因为您有use strict 或其派生词。
错误发生是因为您的程序调用了超出范围的变量。
您可能在子过程/函数中使用了 my 或 local,但正试图在另一个过程或函数调用之外使用它。
sub foo{
my $bar=0;
our ($soap) = 1;
}
foo();
print $bar , "\n"; # does not work w/ strict -- bar is only in the scope of the function, not globally defined
print $main::bar , "\n"; # will run, but won't be populated
print $soap , "\n"; # does not work w/ strict -- the package isn't defined
print $main::soap , "\n"; # will run and work as intended because of our
【讨论】:
使用不带use feature "state" 或use v5.10 的状态变量,除非关键字写为 CORE::state 。
【讨论】:
事实上,如果你在语句末尾使用use strict; 并且在某个地方错过了;,那么以下语句(它们具有完美的语法)可能会引发全局符号需要显式包名 也是。
【讨论】:
您正在使用use strict; 语句,这意味着您的代码必须符合编写 perl 命令的规定。
【讨论】: