Perlbrew

Fork me on github!!

Perlbrew In Shell Scripts

It is generally easy to bootstrap perlbrew and use perlbrew function in shell scripts. In an interactive script, this preamble is usually what's needed:

#!/bin/bash
source $HOME/perl5/perlbrew/etc/bashrc
perlbrew use perl-5.28.0

It is just the same as what's put in bashrc and executed everytime you open a new shell.

However, this may not work for a cron script -- because cron scripts are executed without variables like $HOME. The alternative shell preamble for cron scripts is something like:

#!/bin/bash
export PERLBREW_ROOT=/home/gugod/perl5/perlbrew
export PERLBREW_HOME=/home/gugod/.perlbrew
source ${PERLBREW_ROOT}/etc/bashrc
perlbrew use perl-5.28.0

The $PERLBREW_ROOT and $PERLBREW_HOME are special variables recognized by perlbrew for bootstrapping purposes. They are also used for specifying alternative installation directiory and let multiple accounts on the same machine share the same perlbrew installation.

In an interactive shell environment, the default value of $PERLBREW_HOME is $HOME/.perlbrew. If you don't use the lib feature in perlbrew, you can probably also skip setting this variable.

If this shell script only invokes one program and do not call perlbrew use several times, it can be simplifed by utilizing perlbrew exec command to something like this:

#!/bin/bash
exec /home/gugod/perl5/perlbrew/bin/perlbrew exec -q --with perl-5.28.1 \
    perl /apps/myapp/bin/myapp.pl $*

This runs perl /apps/myapp/bin/myapp.pl, passing through all command-line arguments, after switching perl-5.28.1. The perlbrew executable itself should be located within an initialized $PERLBREW_ROOT directory, as that is how it figures out the installation paths of perls. This last trick does not work with perlbrew instaledd via cpan clients.