Perl "Can't locate object method X via package Y" in CI
Perl resolved the package name but could not find the method in that package or any parent in its @ISA. The class is often not fully loaded, is the wrong version, or the method was renamed.
What this error means
The program dies with "Can't locate object method \"new\" via package \"Foo::Bar\" (perhaps you forgot to load \"Foo::Bar\"?)" at runtime.
perl
Can't locate object method "new" via package "LWP::UserAgent"
(perhaps you forgot to load "LWP::UserAgent"?) at script.pl line 12.Common causes
The class was referenced but never loaded
You called LWP::UserAgent->new without use LWP::UserAgent;, so the package symbol table has no such method.
A version installed in CI lacks or renamed the method
The distribution resolved to a different version than expected, in which the method does not exist or moved to another class.
How to fix it
Load the class before calling it
- Add an explicit
usefor the package that defines the method. - Confirm the distribution that provides that class is installed in CI.
- Re-run so the method is present in the symbol table.
script.pl
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;Pin the version that defines the method
If the method exists only in a specific release, pin it so CI resolves the right version.
cpanfile
requires 'LWP::UserAgent', '>= 6.05';How to prevent it
- Explicitly
useevery class whose methods you call. - Pin versions in a cpanfile so the method surface is stable in CI.
- Run under
use strict; use warnings;to catch typos early.
Related guides
Perl "Can't call method X on an undefined value" in CIFix Perl "Can't call method X on an undefined value" in CI - you invoked a method on a variable that is undef…
Perl "Can't locate X.pm in @INC" in CIFix Perl "Can't locate Foo/Bar.pm in @INC" in CI - the interpreter searched every @INC directory and the requ…
Perl "X version 1.23 required" (this is only 1.10) in CIFix Perl "Foo version 1.23 required--this is only version 1.10" in CI - a `use Foo 1.23` demands a newer rele…