All posts

Fix Missing Import in Perl

Resolve 'Can't locate module' and 'Undefined subroutine' errors in Perl, covering use, require, CPAN modules, and @INC issues.

Missing Import Errors in Perl

Perl's Can't locate X.pm in @INC and Undefined subroutine &X::y called errors indicate missing modules or incorrect imports.

Install the Module

# Using cpanm (recommended)
cpanm JSON::XS
cpanm Moo

# Using CPAN shell
cpan install JSON::XS

use vs require

# use — compile-time import (preferred)
use JSON::XS;
use List::Util qw(sum max min);

# require — runtime import
require JSON::XS;
# Must call with full package name:
JSON::XS::encode_json($data);

@INC Path Issues

If the module exists but Perl can't find it:

# Add a directory to the search path
use lib '/path/to/my/modules';
use lib './lib';

use MyApp::Utils;  # Found in ./lib/MyApp/Utils.pm

Or from the command line:

perl -I./lib script.pl

Importing Specific Functions

# Import nothing (OO interface)
use HTTP::Tiny;
my $http = HTTP::Tiny->new;

# Import specific functions
use File::Path qw(make_path remove_tree);
make_path('a/b/c');

# Import everything (avoid)
use POSIX;  # Imports hundreds of symbols

Version Requirements

use Scalar::Util 1.50 qw(blessed weaken);
# Dies if Scalar::Util < 1.50

Carton (Perl's bundler)

# cpanfile
requires 'Mojo::Pg', '4.27';
requires 'JSON::XS';

# Install
carton install
carton exec perl app.pl

Bugsly captures runtime Can't locate errors with the full @INC path, helping you diagnose module installation issues in production containers.

Try Bugsly Free

Track up to 100 issues per month on the free plan, with unlimited events and no credit card required.

Get Started Free