Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

“Invest in yourself — your confidence is always worth it.”

Explore Cosmetic Hospitals

Start your journey today — compare options in one place.

Perl use vs require: Compile-Time and Runtime Module Loading Explained

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.

Difference between use and require in Perl

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

Aspectuserequire
Execution timeCompile time (via implicit BEGIN)Runtime (when statement is reached)
Expected formModule name (bareword), optional import listModule/file form, including expressions
Imports symbolsYes, by defaultNo (must call import explicitly if needed)
Conditional loadingNot the usual patternCommon and straightforward
Repeat load behaviorUses %INC tracking under the hoodChecks %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 require imports symbols automatically (it does not).
  • Using require for a variable module name without converting it to a file path form.
  • Forgetting that load timing differs: use at compile time, require at runtime.
  • Confusing do with module loading semantics; it serves a different purpose and should be used intentionally.

Best practices

  • Use use for normal, always-required dependencies.
  • Use use Module (); when you want compile-time loading without imports.
  • Use require for optional, delayed, or environment-specific dependencies.
  • Document module-loading choices in shared scripts so future maintainers understand the timing and import behavior.

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
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.

Related Posts

Perl Punctuation and Sigils: Practical Symbol Reference with Examples

A clear reference to the punctuation marks you see most often in Perl code, including sigils, operators, delimiters, and quote forms. Includes usage tables, example snippets, and a quick decision flow for choosing the right sigil.

Read More

Executing External Commands in Perl: system, exec, backticks, and open Explained

Perl provides multiple ways to run external commands, and each one behaves differently. This guide shows when to use system, exec, backticks, or open, with safe patterns for output capture and exit-code handling.

Read More

Perl File Test Operators Explained for DevOps Automation

Perl’s built-in file test operators let you check existence, type, size, permissions, and ownership without external commands. This guide explains the operators, real vs effective ID checks, and production-friendly patterns for automation scripts.

Read More

Perl in DevOps: Practical Overview, Use Cases, and Trade-offs

Perl remains a practical language for text-heavy automation, systems glue code, and legacy infrastructure work. This guide explains where Perl fits, how it compares with Bash and Python, and how to get started with real commands.

Read More

What is Perl and use cases of Perl?

What is Perl? Perl is a high-level, general-purpose programming language known for its flexibility, ease of use, and powerful text manipulation capabilities. Originally developed by Larry Wall…

Read More

What is Perl and How it works? An Overview and Its Use Cases

Hey, this is Ashwani, and in this article I’m going to share some important things about Perl. As you see in the heading everything is clear to…

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x