Some links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.
For reusable Perl code, put it in a module and load it with use Module::Name;. For a plain local Perl file, use require "./file.pl";. Use do when you deliberately want to execute a file again, such as a trusted configuration file. These mechanisms differ in when they run, how they find files, and whether they reload them; Perl does not have one universal PHP-style include.
Choose the right way to load a file
| Mechanism | Example | When it runs | Typical use |
|---|---|---|---|
use |
use My::Utils; |
During compilation | Required modules and reusable code |
require |
require "./inc.pl"; |
When execution reaches it | Conditional module loading or legacy Perl files |
do |
do "./config.pl"; |
When execution reaches it | Files you intentionally want to evaluate again |
use and require normally avoid loading a successfully loaded file again; Perl tracks loaded files in %INC. do does not provide this once-only behavior. Details: use, require, and do.
Recommended: create a module and use it
For shared functions, make a module rather than treating another script as text to paste into the current one. A module name maps to a path: My::Utils is normally stored as My/Utils.pm beneath a directory in Perl’s module search path.
The Tool Desk
Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →# lib/My/Utils.pm
package My::Utils;
use strict;
use warnings;
use Exporter qw(import);
our @EXPORT_OK = qw(greeting);
sub greeting {
return "Hello";
}
1;
The final 1; makes the module return a true value, as required by require—the mechanism behind use. Then load the module and explicitly import only what you need:
#1 Best Overall
#!/usr/bin/env perl
use strict;
use warnings;
use lib 'lib';
use My::Utils qw(greeting);
print greeting(), "n";
To avoid importing a name, call the fully qualified function instead: My::Utils::greeting(). Explicit imports or qualified calls make dependencies clear and reduce naming collisions. You can also request a module version with syntax such as use My::Utils 1.20;, if the module declares a version.
use Module::Name; takes a module name, not a quoted filename. use "filename.pl"; is not the way to load an arbitrary Perl file. use acts at compile time (conceptually like a BEGIN-time require followed by an import), so put any use lib path setup before the module’s use. See Perl’s module documentation.
Loading a plain Perl file with require
If you need to load a legacy .pl file, name its path explicitly:
Rank #2
- Used Book in Good Condition
# inc.pl
our $name = "Arun";
1;
# main.pl
use strict;
use warnings;
require "./inc.pl";
print $name, "n";
require loads and compiles the file when the statement runs. It raises an error if the file cannot be found or compiled, or if its final result is false. The conventional final 1; avoids the last case. A bare require "inc.pl"; searches @INC; it does not reliably mean “the file next to this script.”
You can also use module-name form at runtime: require My::Utils; searches for My/Utils.pm in @INC. A conditional load is a common reason to choose require over use:
if ($feature_enabled) {
require Optional::Feature;
Optional::Feature->run();
}
For optional dependencies, failures can be caught with eval and reported via $@:
Rank #3
my $loaded = eval {
require Optional::Feature;
1;
};
if (!$loaded) {
die "Optional::Feature could not be loaded: $@";
}
A missing file or a compilation/runtime failure will be reflected in the exception text; for operating-system file errors, $! may also be relevant. A false return from the loaded file is a separate require failure. Consult the require reference.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Why a my variable in the other file is unavailable
Loading a file does not make every variable in it global. A variable declared with my is lexical: its visibility is limited to its lexical scope. For example, this does not provide $name to the requiring script:
# inc.pl
use strict;
use warnings;
my $name = "arun";
Changing my to a package variable can make shared state accessible, but it is usually better to expose behavior through a function.
Rank #4
Quick fix: use a package variable
# Shared.pl
package Shared;
use strict;
use warnings;
our $name = "arun";
1;
use strict;
use warnings;
require "./Shared.pl";
print $Shared::name, "n";
our declares a package variable, and the fully qualified name shows which package owns it. This works, but shared mutable globals can make dependencies and tests harder to manage.
Better: expose a function
# Shared.pm
package Shared;
use strict;
use warnings;
sub name {
return "arun";
}
1;
use strict;
use warnings;
use Shared;
print Shared::name(), "n";
Or export the function explicitly with Exporter and call use Shared qw(name);. Prefer this kind of module interface to removing my or exporting every symbol by default.
Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallPaths, @INC, and files beside your script
./inc.pl is relative to the process’s current working directory, which may differ from the directory containing the main script. If a script may be launched from different directories, use FindBin to build a path relative to the script:
Best Value
use FindBin qw($Bin);
require "$Bin/inc.pl";
For project modules in a local lib directory:
use FindBin qw($Bin);
use lib "$Bin/lib";
use My::Utils qw(greeting);
Typical layout:
project/
├── bin/
│ └── app.pl
├── lib/
│ └── My/
│ └── Utils.pm
└── t/
If the application script is in bin/, adjust the path accordingly, for example use lib "$Bin/../lib";. The package name, directory path, and capitalization must agree, particularly on case-sensitive filesystems. use lib adds a directory to @INC; environment configuration such as PERL5LIB can also affect module search paths, but explicit project setup is often easier to understand and reproduce.
To inspect the current search path, run:
perl -e 'print join("n", @INC), "n"'
Do not rely on the current directory being in @INC. A Can't locate ... in @INC error usually means the target is not in a searched directory, the module path does not match its name, the script was launched from a different working directory, or a configured library path is wrong. The @INC and %INC documentation explains the relevant variables.
Using do for configuration or deliberate reloads
do FILE reads, compiles, and executes a file. Each call evaluates it again, so it can suit a simple configuration file that must be reloaded. Unlike require, it does not automatically die for every failure; inspect its result and the error variables:
Free tools Windows power users keep installed
One-click scans. No signup required.
my $result = do "./config.pl";
die "Could not read config.pl: $!" unless defined $result;
die "Could not compile config.pl: $@" if $@;
die "config.pl returned false" unless $result;
A configuration file loaded this way is executable Perl code, not passive data. Only execute files you trust and protect from unauthorized changes. Never build a require or do path directly from unvalidated user input. If configuration should be data rather than code, use a data format such as JSON, YAML, or TOML with an appropriate parser, or use environment variables.
Common errors and checks
Can't locate ... in @INC: Check the path, working directory,use libsetup, package-to-file mapping, and capitalization. Use./file.plfor a path relative to the working directory orFindBinfor one relative to the script.did not return a true value: Ensure the loaded file’s final expression returns true; conventionally end it with1;.- Variable unavailable or “Global symbol requires explicit package name”: A lexical
myvariable is not shared with the caller. Use a module subroutine, or, if necessary, a package variable accessed with its package name. Undefined subroutineafter loading: Check that the file defines the function in the expected package and that you are calling it with the right fully qualified name or importing it explicitly.- Unexpected early loading: That is normal for
use. Use runtimerequirewhen the load must be conditional.
Useful checks include perl -c main.pl for syntax and compilation and perl -V for Perl’s version and configuration. use strict; and use warnings; help expose mistakes; disabling strictness is not a sound fix for scope or namespace problems.
One more distinction: templates are not Perl libraries
If by “include” you mean inserting an HTML header or text fragment into rendered output, use the include directive of your template engine. Template syntax such as [% INCLUDE header %] belongs to that engine, not Perl’s use, require, or do file-loading mechanisms. See this overview of embedding Perl in web pages.
Quick Recap
Decision checklist
- Reusable code the program needs? Build a
.pmmodule and useuse. - Optional dependency or legacy library loaded only under a condition? Use
require. - Trusted Perl configuration that must be evaluated again? Consider
do, with explicit error checks. - Untrusted file or user-provided path? Do not execute it with
requireordo. - HTML or text template fragment? Use the template engine’s include feature.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

