Setting the Environment Variable
PERL5LIBPerl 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: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: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:# Linux/macOS
export PERL5LIB=/first/path:/second/path:$PERL5LIB
An Alternative: The
use lib PragmaIf 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:use strict;
use warnings;
use lib '/path/to/your/custom/modules';
# Now you can use your custom module
use MyCustomModule;
No comments:
Post a Comment