Skip to content
Latchkey

Perl "Can't call method X on an undefined value" in CI

The invocant was undef when you called a method on it. In CI this usually means a constructor, config lookup, or database handle returned undef because a resource (env var, service, file) was absent.

What this error means

The program dies with "Can't call method \"execute\" on an undefined value at script.pl line N", where the object should have been created earlier.

perl
Can't call method "execute" on an undefined value at db.pl line 18.

Common causes

A prior call returned undef in CI

A connect, prepare, or config accessor returned undef (missing env var, unreachable service) and you called a method on that undef.

An unchecked constructor failure

A constructor that returns undef on failure was not checked, so the next method call dereferences undef.

How to fix it

Check the value before calling a method

  1. Add a guard or or die on the call that produced the object.
  2. Print the error variable ($@, $DBI::errstr) to see why it was undef.
  3. Provide the missing env var or service in the CI job.
script.pl
my $sth = $dbh->prepare($sql) or die "prepare failed: " . $dbh->errstr;
$sth->execute;

Supply the missing CI resource

If the undef came from a missing environment value, inject it in the step env so the object is created.

.github/workflows/ci.yml
env:
  DATABASE_URL: ${{ secrets.DATABASE_URL }}

How to prevent it

  • Guard every constructor and connect with or die.
  • Provide required env vars and services to the CI job explicitly.
  • Run with use strict; use warnings; to surface undef usage.

Related guides

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