Global symbol requires explicit package name

后端 未结 6 1438
野的像风
野的像风 2020-12-01 10:38

Global symbol requires explicit package name? Why has this occurred and what are various cases that can cause this error?

相关标签:
6条回答
  • 2020-12-01 10:53

    In order to specifically say what caused it in your code, you would need to post your code.

    The error is outputted and your script is stopped because you've got use strict or a derivative of it. The error occurs because your program is calling a variable out of scope.

    1. You may have used my or local inside a sub procedure/function, but are trying to use it inside another procedure, or outside the function call.

       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
      
    0 讨论(0)
  • 2020-12-01 11:00

    You are using the use strict; statement meaning your codes have to be within the regulations of writing perl commands.

    0 讨论(0)
  • 2020-12-01 11:06

    It may also happen when the statement before is not complete.

    use strict;
    
    sub test;
    
    test()
    
    # some comment
    my $x;
    

    perl now complains with following error message:

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

    The error is not in the declaration of "my", but at the missing semicolon (;) at test().

    0 讨论(0)
  • 2020-12-01 11:07

    using state variables without use feature "state" or use v5.10 unless the keyword is written as CORE::state .

    Taken from http://perldoc.perl.org/functions/state.html

    0 讨论(0)
  • 2020-12-01 11:13

    In fact, if you use use strict; and somewhere you miss ; at the end of a statement, then the following statements (they have perfect syntax) may raise Global symbol requires explicit package name as well.

    0 讨论(0)
  • 2020-12-01 11:15

    Have a look at perldiag:

    Global symbol "%s" requires explicit package name

    (F) You've said "use strict" or "use strict vars", which indicates that all variables must either be lexically scoped (using "my" or "state"), declared beforehand using "our", or explicitly qualified to say which package the global variable is in (using "::").

    0 讨论(0)
提交回复
热议问题