2010-06-29 45 views
24

全局符号需要显式包名?为什么会发生这种情况,以及可能导致此错误的各种情况是什么?全局符号需要显式包名

+1

示例Perl代码?请参阅http://stackoverflow.com/q/4257179/10468或在perl模块上搜索问题。另请参见(异地)http://www.sitepoint.com/forums/showthread.php?501379-Perl-require-and-variables – DarenW 2012-04-12 17:31:42

回答

22

看一看perldiag

全球符号 “%s” 需要明确的包名

(F)你说: “使用严格” 或 “使用严格瓦尔”这表明所有变量必须是词法范围的(使用“my”或“state”),事先使用“our”声明,或明确限定全局变量所在的包(使用“::”)。

5

为了具体说明是什么导致它在你的代码中,你需要发布你的代码。

的错误是输出和你的脚本停止,因为你有use strict或它的衍生物。 错误发生因为您的程序正在调用超出范围的变量。

  1. 您可能在子程序/函数中使用了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 
    
-1

您正在使用use strict;声明意味着你的代码必须是书面形式的Perl命令的规定之内。

+2

写入Perl命令的哪些规定被违反? – alex 2017-07-18 18:32:15

23

当以前的陈述未完成时也可能发生。

use strict; 

sub test; 

test() 

# some comment 
my $x; 

的Perl现在抱怨与以下错误消息:

my " 
Global symbol "$x" requires explicit package name 

的错误不是在宣告 “我的”,但在在test()缺少分号(;)。

+2

谢谢!我正在撕掉我的头发。 – Zack 2016-09-01 23:52:35

0

其实两者,如果你使用use strict;和地方你在声明的结尾错过;,那么下面的语句(他们有完善的语法)可能会提高全局符号需要明确包名以及。

相关问题