Modules & CPAN
Learn how to use Perl modules with the use keyword, install third-party modules with cpan and cpanm, and explore a few well-known CPAN modules.
Introduction
You have already used modules without necessarily calling them that — every use strict; and use warnings; at the top of your scripts loads a module. Modules are reusable packages of Perl code, and Perl's ecosystem of third-party modules, CPAN, is one of the largest and oldest in any programming language, with hundreds of thousands of modules covering nearly every imaginable task.
- What a module is and how use loads it.
- What CPAN is and why it matters.
- How to install third-party modules with cpan and cpanm.
- A few essential, widely used CPAN modules.
What Is a Module?
A module is simply a Perl file (with a .pm extension) that packages up related subroutines, variables, and sometimes object-oriented classes, so they can be reused across multiple scripts. Modules come in three broad categories: core modules that ship with every Perl installation, third-party modules from CPAN that you install separately, and your own custom modules for organizing a larger project.
The use Keyword
use loads a module at compile time, before the rest of the script runs, and typically imports specific functions or symbols into your script's namespace. You have already seen this pattern many times with use strict; and use warnings;.
use strict;use warnings;use List::Util qw(sum max min);
my @numbers = (4, 8, 15, 16, 23, 42);
print "Sum: ", sum(@numbers), "\n";print "Max: ", max(@numbers), "\n";print "Min: ", min(@numbers), "\n";Click Run to see what this code prints.
List::Util is a core module (it ships with Perl itself, no separate installation required) that provides handy functions like sum, max, min, and first, which would otherwise require writing manual loops every time.
What Is CPAN?
CPAN stands for the Comprehensive Perl Archive Network. It is a massive, community-maintained repository of Perl modules covering everything from web frameworks and database drivers to date parsing, JSON encoding, and testing frameworks. When you need functionality that Perl's core does not provide, CPAN is almost always the first place to look.
Installing Modules
Perl ships with a built-in tool called cpan for installing modules from CPAN, but most Perl developers today prefer cpanm (App::cpanminus), a lightweight, faster, and far less chatty alternative that handles dependencies automatically.
# Using the built-in cpan clientcpan JSON
# Using cpanm (recommended - install it once, then use it for everything)cpanm JSONcpanm DateTimecpanm Text::CSVClick Run to see what this code prints.
If cpanm is not already available, most systems can bootstrap it with a single command: cpan App::cpanminus, or by following the installer script documented on the cpanminus CPAN page.
Well-Known CPAN Modules
A handful of CPAN modules show up in an enormous share of real-world Perl code. Getting familiar with these will save you from reinventing solutions to already-solved problems.
| Module | Purpose |
|---|---|
| JSON / JSON::PP | Encode and decode JSON data. |
| DateTime | Robust, timezone-aware date and time handling. |
| Text::CSV | Properly parse and write CSV files, handling quoting and edge cases List of manual splitting misses. |
| LWP::UserAgent | Make HTTP requests to web services and APIs. |
| DBI | A unified database interface for connecting to MySQL, PostgreSQL, SQLite, and more. |
| Moo / Moose | Modern, streamlined tools for building object-oriented Perl classes. |
use strict;use warnings;use JSON;
my %data = (name => 'Alice', age => 30, active => 1);
my $json_text = encode_json(\%data);print "$json_text\n";
my $decoded = decode_json($json_text);print "Decoded name: $decoded->{name}\n";Click Run to see what this code prints.
Writing Your Own Module
You can package your own reusable code into a module too. A minimal module is just a package block ending in a true value (1;), saved with a .pm extension matching its package name.
# File: MathHelpers.pmpackage MathHelpers;use strict;use warnings;use Exporter 'import';
our @EXPORT_OK = ('double');
sub double { my ($n) = @_; return $n * 2;}
1; # modules must end with a true value# File: main.pluse strict;use warnings;use MathHelpers qw(double);
print double(21), "\n";Click Run to see what this code prints.
Common Mistakes
- Forgetting that a custom module must end with a true value (typically 1;), or Perl will refuse to load it.
- Confusing the built-in cpan client with cpanm — they are different tools, though both install from CPAN.
- Not qualifying which functions to import, like use List::Util qw(sum max);, and being surprised a function is unavailable.
- Installing modules without checking whether a lighter-weight core module already does the job (e.g. reaching for a heavy module when List::Util already has what you need).
- Forgetting to add newly installed CPAN modules to a project's dependency list (such as cpanfile), so a deployment on another machine fails.
Best Practices
- Prefer cpanm over the classic cpan client for faster, quieter, dependency-aware installs.
- Check if a core module (List::Util, Scalar::Util, Data::Dumper, POSIX) already solves your problem before installing a third-party one.
- Explicitly list only the functions you need when importing, like use List::Util qw(sum max);, to keep your namespace clean.
- Pin dependency versions in a cpanfile for reproducible deployments across environments.
- Read a module's documentation on metacpan.org before adopting it, and check its last update date and popularity.
Frequently Asked Questions
Core modules ship with every standard Perl installation (like List::Util or Data::Dumper) and need no separate install. CPAN modules are third-party and must be installed explicitly with a tool like cpanm.
metacpan.org is the standard web interface for searching CPAN, reading documentation, and checking a module's popularity and maintenance status before installing it.
It depends on your system configuration — some setups install modules system-wide (may need admin rights), while tools like local::lib or perlbrew let you install modules into your own user directory without elevated permissions.
Perl requires the last statement evaluated when a file is use'd to be true. The bare 1; is a simple, conventional way to guarantee that.
Key Takeaways
- A module is a reusable .pm file of Perl code, loaded into a script with use.
- CPAN is Perl's enormous, community-run archive of third-party modules.
- cpanm is the modern, recommended tool for installing modules from CPAN.
- JSON, DateTime, Text::CSV, LWP::UserAgent, and DBI are essential, widely used CPAN modules.
- A custom module must end in a true value, typically the bare statement 1;.
Summary
Modules and CPAN let you avoid reinventing the wheel and give you access to decades of battle-tested, community-maintained Perl code. To close out this part of the course, the next lesson shows you how Perl approaches object-oriented programming using packages, bless, and method calls.