全局符号需要显式包名称

Tre*_*ree 27 perl

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

mus*_*iKk 25

看看perldiag:

全局符号"%s"需要显式包名称

(F)您已经说过"use strict"或"use strict vars",这表明所有变量必须是词法范围(使用"my"或"state"),事先使用"our"声明,或明确限定为说出全局变量所在的包(使用"::").


kop*_*por 25

当之前的陈述未完成时,也可能发生这种情况.

use strict;

sub test;

test()

# some comment
my $x;
Run Code Online (Sandbox Code Playgroud)

perl现在抱怨以下错误消息:

my "
Global symbol "$x" requires explicit package name
Run Code Online (Sandbox Code Playgroud)

错误不在"我的"声明中,而是在缺少的分号(;)处test().

  • 我在一行之前漏掉了一个分号,这就是我需要的答案,谢谢 (2认同)

vol*_*ron 7

为了具体说明代码中的原因,您需要发布代码.

输出错误并且您的脚本已停止,因为您已获得use strict或衍生它.发生此错误因为您的程序正在调用超出范围的变量.

  1. 您可能在子过程/函数中使用了我的或本地,但是尝试在另一个过程中或在函数调用之外使用它.

     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
    
    Run Code Online (Sandbox Code Playgroud)