Wednesday, April 13, 2011

How to Set the PERL5LIB Environment Variable


Setting the Environment Variable PERL5LIB

Perl will look for modules in the directories specified in the PERL5LIB environment variable before looking in the standard library and current directory. Setting this variable is one of the easiest ways to instruct Perl to locate your custom or locally installed modules without modifying your script's code.

How to Set PERL5LIB in Linux and macOS (Bash/Zsh)
To set the variable for your current terminal session, use the export command. You can append your new path to any existing PERL5LIB paths to avoid overwriting them:

Bash
export PERL5LIB=/path/to/your/custom/modules:$PERL5LIB
To make this permanent, add the above line to your ~/.bashrc or ~/.zshrc file.

How to Set PERL5LIB in Windows (Command Prompt)
On Windows systems, you use the set command. Note that Windows uses a semicolon (;) to separate multiple paths instead of a colon:

DOS
set PERL5LIB=C:\path\to\your\custom\modules;%PERL5LIB%

Adding Multiple Directories
If you have modules spread across multiple directories, you can include all of them in PERL5LIB by stringing them together with the appropriate path separator for your operating system:

Bash
# Linux/macOS
export PERL5LIB=/first/path:/second/path:$PERL5LIB

An Alternative: The use lib Pragma
If you prefer not to rely on environment variables, or if you are running scripts on a server where you cannot change the environment, you can define the module path directly inside your Perl script using the lib pragma:

Perl
use strict;
use warnings;
use lib '/path/to/your/custom/modules';

# Now you can use your custom module
use MyCustomModule;

No comments: