TL;DR

A shell function to manipulate PERL5LIB for a local library to a project.

It’s no secret that I like a local directory to Install Perl Modules, which made me think of a possible solution to use those “private” libraries for commands that I want to have easy access to (A cheap trick to manipulate PERL5LIB).

Other times, though, I just like to have some modules installed locally in a directory where I’m experimenting with something. For these cases, I find it useful the following shell function:

local-lib() { 
    PERL5LIB="$PWD/local/lib/perl5";
    while [ $# -gt 0 ]; do
        local path="$(readlink -f "$1")";
        shift;
        PERL5LIB="$path:$PERL5LIB";
    done;
    export PERL5LIB
}

When called inside a directory, it sets PERL5LIB to the local/perl5/lib (using the absolute path, which happens after til). If passed additional parameters, it adds them to PERL5LIB too, which is handy if I’m trying out some new modules placed inside the lib directory. This allows me to call this:

$ cd /somewhere

$ local-lib lib

$ printf '%s\n' "$PERL5LIB"
/somewhere/local/perl5/lib:/somewhere/lib

It’s importan that the function is kept as a shell function - otherwise, it will not set PERL5LIB in the current shell, but in a subshell that will be lost after the program exits. For this reason, I keep it in ~/.bashrc.

I guess it’s all! Have a nice day and stay safe!