While Python as the extremely useful virtualenv tool, I haven’t found anything similar for Perl. What I want is quite simple: being able to test random perl tools. They usually depend on a various set of libraries, some of them either too old on my system or not even present. Most of the time, I just want to test the tool, so installing random perl modules system-wide is not an option. Moreover, I really don’t like having to install something system-wide which is not coming as a package for my distribution. The solution is to have a virtual environment where I can install all the libraries I want, somewhere in my user directory. This has the advantage too of allowing to use a different version of a library for different perl tools.
While I haven’t found an all-in-one tool for perl, there’s the CPAN module called local-libwhich helps a lot. You can find its documentation here. Here is how to use it to create a virtual perl environment:
${PERLENV}
represents the directory where you want to have your perl virtual env. E.g.: export PERLENV="$HOME"/perl5
mkdir -p "${PERLENV}" cd "${PERLENV}"
wget -O local-lib.tar.gz "${TARBALL_URL}" tar xvf local-lib.tar.gz cd local-lib-*
perl Makefile.PL --bootstrap="${PERLENV}"
Would you like me to configure as much as possible automatically? [yes]
make test && make install
The environment is now more or less setup. However, to use it, you need to set a few environment variables. Here are 2 helpers for that:
cat > $PERLENV/perl << EOF #!/bin/sh PERLENV="${PERLENV}" perl -I\$PERLENV/lib/perl5 -Mlocal::lib=\$PERLENV -MCPAN "\$@" EOF chmod 755 $PERLENV/perl
"${PERLENV}"/activate
and then anything that needs perl in the current shell will use your custom environment. cat > "${PERLENV}"/activate << EOF PERLENV="${PERLENV}" eval \$(perl -I\$PERLENV/lib/perl5 -Mlocal::lib=\$PERLENV -MCPAN "\$@") EOF
Tadam, you local perl is now usable. You can start installing the CPAN modules you want and they will be installed in the environment:
"${PERLENV}"/perl -MCPAN -e 'CPAN::install(POE::Filter::IRCD)'
Or the CPAN shell:
"${PERLENV}"/perl -MCPAN -e shell
And if you want to install your favorite perl tool:
source "${PERLENV}"/activate cd myfavoritetool ./runit
Various notes:
~/.cpan/
configuration directory, so you cannot have CPAN parameters specific to your environment. AFAIK, there’s no easy way around that.