Skip to content
Can't call method '...' on an undefined value

Can't call method '...' on an undefined value

DodaTech 3 min read

The “Can’t call method ‘…’ on an undefined value” error means you called a method on a variable that is undef instead of a blessed object or reference.

What It Means

In Perl, methods can only be called on blessed references (objects) or class names. When you write $object->method(), Perl expects $object to be a reference that has been blessed into a class. If $object is undef — never assigned, returned from a failed operation, or explicitly set to undef — Perl cannot dispatch the method call and throws this fatal error.

Why It Happens

  • A database query returned undef instead of a row object.
  • A constructor call failed but you did not check the return value.
  • A function returned undef on error but the caller assumed success.
  • You assigned undef to a variable earlier and forgot about it.
  • A hash key or array element does not exist and you accessed it without checking.
  • You used $self in a class method where $self is not defined.

How to Fix It

1. Check the return value of constructors

use strict;
use warnings;

# Bad — no error checking
my $user = User->new();   # may return undef on failure
$user->name();            # "Can't call method 'name' on an undefined value"

# Good — check the return value
my $user = User->new();
if (defined $user) {
    print $user->name();
} else {
    warn "Failed to create user";
}

2. Use defined() before method calls

my $result = get_record($id);
if (defined $result) {
    $result->process();
} else {
    warn "No record found for ID $id";
}

3. Use the safe dereference operator (->? or //)

Perl 5.10+ offers the defined-or operator:

my $user = get_user($id);
($user //= User->new())->display();   # Use a default object if undef

4. Debug with Data::Dumper to inspect values

use Data::Dumper;

my $object = some_function();
print Dumper($object);   # If it prints '$VAR1 = undef;', you know the issue

# Then trace back to find why some_function returned undef

5. Check hash/array access before method calls

my %data = (name => "Alice");

# Bad — 'email' key doesn't exist
$data{email}->send();

# Good — check the key exists
if (exists $data{email} && defined $data{email}) {
    $data{email}->send();
}

# Or use a conditional
my $email = $data{email};
$email->send() if defined $email;
How do I distinguish between undef and an empty string?
Use defined()defined "" returns true (empty string is defined), while defined undef returns false. An empty string can still have methods called on it if it’s blessed, but typically string values are not objects. Use ref() to check if something is a reference before calling methods.
Can I call methods safely without checking every time?
Yes — Perl 5.10+ has the // (defined-or) operator for defaults, and Perl 5.36+ added the $object?->method() safe dereference syntax (experimental). Alternatively, use eval or Try::Tiny to catch the error: eval { $object->method() }; warn $@ if $@;.
Is this error specific to object-oriented Perl?
No — it happens with any method call on undef, including calls on DBI statement handles, filehandles, and Moose/Moo objects. The same pattern of checking defined() before calling methods applies regardless of the framework or module you are using.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro