Originally published on 2012-04-05; fully revised and updated.
If you maintain Perl in automation, CI/CD glue scripts, or legacy operations tooling, choosing between use and require affects correctness and startup behavior. This article explains what each keyword does, when code is loaded, and how to avoid subtle module-loading mistakes.

What use and require do
use is primarily a compile-time convenience. In practical terms, use Module LIST; behaves like a compile-time block that loads the module and then calls import for you. require loads code at runtime when execution reaches that line, and it does not import symbols automatically.
use Some::Module qw(func1 func2);
BEGIN {
require Some::Module;
Some::Module->import(qw(func1 func2));
}Code language: PHP (php)
If you want compile-time loading without importing, use use Module ();.
use vs require comparison
| Aspect | use | require |
|---|---|---|
| Execution time | Compile time (via implicit BEGIN) | Runtime (when statement is reached) |
| Expected form | Module name (bareword), optional import list | Module/file form, including expressions |
| Imports symbols | Yes, by default | No (must call import explicitly if needed) |
| Conditional loading | Not the usual pattern | Common and straightforward |
| Repeat load behavior | Uses %INC tracking under the hood | Checks %INC to avoid reloading already loaded files |
Execution flow: compile time vs runtime
flowchart TD
A[Perl starts compilation] --> B[use Module found]
B --> C[Load module now]
C --> D[Call import now]
D --> E[Program enters runtime]
E --> F[require Module reached]
F --> G{Already loaded}
G -- yes --> H[Skip reload and continue]
G -- no --> I[Load module at runtime]
Practical Perl examples
Compile-time dependency with exports
use strict;
use warnings;
use List::Util qw(sum);
my $total = sum(1, 2, 3);
print "$total\n";Code language: PHP (php)
Runtime conditional loading
if ($ENV{ENABLE_HIRES}) {
require Time::HiRes;
my $started = Time::HiRes::time();
print "Started at $started\n";
}Code language: PHP (php)
Dynamic module name from configuration
my $module = $ENV{PLUGIN_MODULE} || 'My::Default::Plugin';
(my $file = "$module.pm") =~ s{::}{/}g;
require $file;
print "Loaded $module from $file\n";Code language: PHP (php)
For dynamic loading, remember that converting :: to / and appending .pm is a common pattern when the module name is stored in a variable.
Commands to verify behavior in your Perl installation
perldoc -f use
perldoc -f require
perldoc -f do
perldoc perlmodCode language: JavaScript (javascript)
Common pitfalls
- Assuming
requireimports symbols automatically (it does not). - Using
requirefor a variable module name without converting it to a file path form. - Forgetting that load timing differs:
useat compile time,requireat runtime. - Confusing
dowith module loading semantics; it serves a different purpose and should be used intentionally.
Best practices
- Use
usefor normal, always-required dependencies. - Use
use Module ();when you want compile-time loading without imports. - Use
requirefor optional, delayed, or environment-specific dependencies. - Document module-loading choices in shared scripts so future maintainers understand the timing and import behavior.
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services — all in one place.
Explore Hospitals