Bash function for directory-dependent local::lib

On my laptop I use perlbrew to keep my system perl separate from my development perl. But I also develop various Perl projects with different dependencies, and would like to keep those dependencies separate if possible. Using a separate perlbrew perl for each project as well would be overkill in terms of diskspace, CPU energy and time so I thought local::lib might be useful.

If I install dependencies for a particular project using "cpanm -l extlib $MODULE" in the top level directory, the function below will automatically setup local::lib for that location when I change into that directory, and unset it when I change out. I use a test for the existence of a "extlib" directory and a Makefile.PL file because that is what I consistently have.



# Automatic local::lib setup based on extlib directory
set_local_lib() {
if [[ $PWD != $MYOLDPWD ]]; then
MYOLDPWD="$PWD";
OLDPERL5LIB=$PERL5LIB

unset PERL5LIB
unset PERL_MB_OPT
unset PERL_MM_OPT
unset PERL_LOCAL_LIB_ROOT

if [[ -n $VERY_LOCAL_LIB_PATH ]]; then
export PATH=`echo $PATH | sed -e "s|$VERY_LOCAL_LIB_PATH:||g"`
unset VERY_LOCAL_LIB_PATH
fi

if [[ -e Makefile.PL && -d extlib ]]; then
eval `perl -Mlocal::lib=extlib`
VERY_LOCAL_LIB_PATH=$PWD/extlib/bin
echo "[Using local::lib with $PWD/extlib]";
fi
if [[ -n $OLDPERL5LIB ]]; then
unset OLDPERL5LIB
echo "[local::lib turned off]";
fi
fi
}

export PROMPT_COMMAND=set_local_lib

Can this be improved? Is there are better way of achieving location dependent local::lib without resorting to FindBin/local::lib trickery in test scripts? It has also been mentioned that perlbrew and local::lib don't work together. If "extlib" was a perl-version-dependent location this could also be overcome.

2 Comments

I started using smartcd (http://smartcd.org/) for this kind of thing.

Leave a comment

About Mark Lawrence

user-pic I blog about Perl.