Skip to content
Global symbol requires explicit package name

Global symbol requires explicit package name

DodaTech 3 min read

The “Global symbol requires explicit package name” error means you used a variable not declared with my, our, or fully qualified with a package name.

What It Means

When use strict is enabled, Perl requires every variable to be explicitly declared. If you write $x = 5 without first writing my $x, Perl does not know whether $x belongs to the current package or is a global variable. The strict pragma forces you to declare variables, preventing accidental globals and silent bugs.

Why It Happens

  • You omitted my when first using a variable: $count = 0 instead of my $count = 0.
  • You misspelled a variable name: declared my $username but later used $user_name.
  • You used use strict but wrote a bare variable assignment without declaration.
  • You forgot to use our for package-level global variables.
  • You are trying to use a variable from another package without fully qualifying it ($Other::var).

How to Fix It

1. Add my before every new variable

use strict;
use warnings;

# Bad — global symbol error
$count = 10;

# Good — declared with my
my $count = 10;

2. Check for typos in variable names

use strict;
my $username = "alice";

# Typo — Perl sees $user_name as a different, undeclared variable
print $user_name;

# Fix — use the correct name
print $username;

3. Use our for package-level globals

When you intentionally want a global variable:

use strict;
use warnings;

package MyConfig;
our $debug = 1;    # Declared as package global

sub show_debug {
    print $debug;  # OK — declared with our
}

4. Fully qualify variables from other packages

use strict;

# Accessing another package's global without qualification
$MyConfig::debug = 1;   # OK — fully qualified

# Or import it
use MyConfig qw($debug);
$debug = 1;

5. Use warnings to catch more issues

use strict;
use warnings;

my $value = 42;
print $vlue;  # 'warnings' would flag this as an uninitialized value
What does use strict actually do?
use strict enables three separate restrictions: strict refs (no symbolic references), strict subs (barewords must be function calls), and strict vars (variables must be declared). The “Global symbol” error comes from the strict vars restriction — it’s enabled with use strict; and covers all three.
Is it possible to turn off strict for one variable?
Yes — use the no strict 'vars' pragma in a small scope: { no strict 'vars'; $temp_var = 1; }. This is occasionally useful for one-liners or legacy code, but you should almost always declare variables with my instead.
What’s the difference between my, our, and local?
my declares a lexically scoped variable (visible only in the current block). our declares a package variable visible in the current scope but accessible globally. local temporarily overrides a global variable’s value within a scope, restoring it afterwards. Use my by default.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro