Skip to content

Perl web development – Step 3: Carton

This is step 3 in the Perl web development series. In this step we’re setting up Carton to manage our Perl package dependencies.

I like my Perl apps self contained. No needs for system wide installed Perl modules these days. Sure there might be cases where it’s useful, but mostly we want little self contained Perl apps. Perfect for running on a shared server. In combination with the earlier mentioned Perlbrew, Carton can add a perfect combination of separated Perls running separated apps without ever interfering with each others base.

Carton

That’s where the awesome Perl module Carton comes in. Carton is a Perl module maintainer. It installs new modules and keeps track of those installs. Easy to deploy on production systems using the specific modules installed for your dev environment so you are sure not to mess up Perl module versions.

Install Carton

Carton needs to be installed in the perl version that you’re using. If you’ve followed my previous article in this series this is as easy as running:

cpanm install 'Carton'

cpanfile

After installing Carton, the next thing to do is create a cpanfile and add your required modules to it. A simple cpanfile installing Dancer2 and Template Toolkit would look like this.

requires "Dancer2" => "0";
requires "Template" => "0";

recommends "YAML" => "0";
recommends "URL::Encode::XS" => "0";
recommends "CGI::Deurl::XS" => "0";
recommends "HTTP::Parser::XS" => "0";
on "test" => sub {
requires "Test::More" => "0";
requires "HTTP::Request::Common" => "0";
}

The ‘=> “0”‘ here means install the latest version. This can be altered to install specific versions of the modules. If you want to install a specific version of Dancer you can do

requires "Dancer2" => "0.204001";

Carton install

The cpanfile is setup and installing the modules is pretty straightforward. Run

carton install

And all the modules will be installed including their dependencies.

The output looks like


Installing modules using /Users/***/Dev/MovieCollection/cpanfile
Successfully installed ExtUtils-MakeMaker-7.24 (upgraded from 6.63_02)
Successfully installed Test-Harness-3.36
Successfully installed ExtUtils-Helpers-0.026
Successfully installed ExtUtils-Config-0.008
Successfully installed ExtUtils-InstallPaths-0.011
Successfully installed Module-Build-Tiny-0.039
......
Successfully installed File-ShareDir-Install-0.11
Successfully installed Test-LeakTrace-0.15
Successfully installed AppConfig-1.71
Successfully installed Template-Toolkit-2.26
94 distributions installed
Complete! Modules were installed into /Users/***/Dev/MovieCollection/local

Error messages? You need to solve those and run the install command again. If you have any problems please feel free to leave a comment, I or another reader might be able to help. Make sure to leave all info necessary in the comment like OS, Perl version and error message.

Published inCode