Skip to content
Latchkey

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

  1. Add an explicit use for the package that defines the method.
  2. Confirm the distribution that provides that class is installed in CI.
  3. 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 use every 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

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →