Global symbol requires explicit package name
Asked Answered
T

7

43

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

Thirst answered 29/6, 2010 at 14:1 Comment(1)
Sample perl code? See https://mcmap.net/q/390650/-why-does-perl-say-global-symbol-quot-symbol-quot-requires-explicit-package-name-at-program-pl-line-x/10468 or search for questions on perl modules. See also (offsite) sitepoint.com/forums/…Jazminejazz
G
38

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 "::").

Gonium answered 29/6, 2010 at 14:19 Comment(1)
You can get this extended message by changing use warnings to use diagnostics in the script.Phosphide
A
38

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().

Adze answered 15/1, 2012 at 14:50 Comment(2)
i missed a semi-colon before one line, and this was the answer i needed thanksBarrybarrymore
@Adze could you please give me a hint how to find the missing semicolonClemmer
C
10

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
    
Caffey answered 30/6, 2010 at 19:56 Comment(0)
F
1

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

Freidafreight answered 2/12, 2015 at 17:6 Comment(0)
E
0

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.

Edlyn answered 31/5, 2016 at 9:1 Comment(0)
E
-3

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

Emissive answered 19/5, 2011 at 13:36 Comment(0)
M
-3

about this error in bash script Global symbol requires explicit package name\

i had a lot of pain with script in bash for rename a text. so i need contribute something
i was adding a text beetwin name file an his extension, so
only export a variable

#!/bin/bash
echo -e "\e[1;33m Path dir to find: \e[0m"; read -r dirclips
echo -e "\e[1;33m Search this file: \e[0m"; read -r item
echo -e "\e[1;33m add this words to the file name (ej. A1): \e[0m"; read -r iditem
c="$iditem"
export c
find "$dirclips" -name "$item" -exec rename 's/(.+)\.(.+)/$1-\Q$ENV{c}\E.$2/' "{}" \;
Manipur answered 29/5 at 7:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.