Common errors

What Global symbol requires explicit package name, Use of uninitialized value, isn't numeric, Can't locate and strict refs mean and how to fix them.

Runs in Zerethon IDE · Perl 5.40 Official Perl documentation ↗

Perl reports problems with the file and line: at main.pl line 3. A compile error stops the program before it starts; a warning is printed and the program goes on — read warnings, they usually point at the real bug.

  • Global symbol "$x" requires explicit package name (did you forget to declare "my $x"?) — declare the variable with my, or fix the typo.
  • Use of uninitialized value $x in concatenation (.) or string — the value is undef: a missing hash key, an index past the end, or input that ran out.
  • Argument "abc" isn't numeric in addition (+) — text used as a number; check it with a regex first.
  • Can't locate Geometry.pm in @INC — add use lib ".";, check the file name and capitals, or the module is from CPAN and not installed.
  • Can't use string ("abc") as a HASH ref while "strict refs" in use — a plain value was used where a reference was expected.
  • syntax error at main.pl line 5, near ... — often a missing ; on the line before, or an unclosed brace.
  • Comparing strings with == — "abc" == "xyz" is true (both are 0 as numbers); use eq.
main.pl
use v5.40;

my $total = 0;
for my $v ("10", "x", "7") {
    try {
        die "not a number\n" unless $v =~ /^-?\d+$/;
        $total += $v;
    }
    catch ($e) {
        chomp $e;
        say "skipped '$v': $e";
    }
}
say "total: $total";

Output

skipped 'x': not a number
total: 17
Handle an error you expect with try / catch (part of use v5.40); older code uses eval { ... } and checks $@.