Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
# 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
Sale
Perl Pocket Reference: Programming Tools
  • Used Book in Good Condition
#!/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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
# 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 $@:

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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
Sale
Learning Perl
  • Used Book in Good Condition

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Paths, @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:

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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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 lib setup, package-to-file mapping, and capitalization. Use ./file.pl for a path relative to the working directory or FindBin for one relative to the script.
  • did not return a true value: Ensure the loaded file’s final expression returns true; conventionally end it with 1;.
  • Variable unavailable or “Global symbol requires explicit package name”: A lexical my variable is not shared with the caller. Use a module subroutine, or, if necessary, a package variable accessed with its package name.
  • Undefined subroutine after 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 runtime require when 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

SaleBestseller No. 1
Perl Pocket Reference: Programming Tools
Perl Pocket Reference: Programming Tools
Used Book in Good Condition
$7.63
SaleBestseller No. 2
SaleBestseller No. 4
Learning Perl
Learning Perl
Used Book in Good Condition
$16.00

Decision checklist

  • Reusable code the program needs? Build a .pm module and use use.
  • 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 require or do.
  • 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.