System setup for modern perl projects

Some helpful tips in making your perl projects more portable.

Installing perl

Don't use the system perl installed with your operating system, instead the latest stable perl via plenv:

Once installed, create a .perl-version in the root of your project containing just the version number of the perl you require, for example:

echo '5.22.0' > .perl-version

Then install that version, as follows:

plenv install 5.22.0

Installing modules

App::cpanminus

The standard cpan command is fine, but does use a fair amount of memory, this can be a problem if running from small virtual machines. I recommend that cpanm is used instead for its simplicity.

This can be installed in one command from plenv as follows (after a perl version has been installed):

plenv install-cpanm

This should only be used to install the Carton module mentioned below as any modules installed with cpanm will be installed globally to the running perl version.

Carton

Use Carton for application dependencies, it also ensures that the same version of modules are installed consistently with its snapshot feature. Modules installed using this tool will be installed in a directory called local inside the correct project, so will not be available globally. To remove all the installed modules the local directory can simply be deleted.

To install:

cpanm Carton

Create an example dependency list:

echo "requires 'Acme::Time::Baby';" >> cpanfile

Install modules listed in cpanfile:

carton install

Documentation

Link to meta::cpan rather than CPAN as module pages on meta::cpan do not use the module version, so less chance of broken links in the future.

Configuration

To find the dependencies that carton has installed please use the following at the top of your scripts:

use FindBin qw/$Bin/;
use local::lib "$Bin/local";

You may also wish to use the following line to find your project libraries:

use lib 'lib';

Last updated: 18/10/2020