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
mywhen first using a variable:$count = 0instead ofmy $count = 0. - You misspelled a variable name: declared
my $usernamebut later used$user_name. - You used
use strictbut wrote a bare variable assignment without declaration. - You forgot to use
ourfor 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 valueBuilt by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro