Perl Dependency management with Carton

The perl depenendency management tool Carton allows not only easily separating dependencies for different applications or parts of the system, but also easily deploying an application and handling its dependencies.
Using Carton is extremely straight-forward for anybody who’s used cpanm to manage dependencies before.

Installing and using your application

The first step is to write a cpanfile:

requires 'LWP', '6.26';
requires 'DBIx::Class', '0.082840';
requires 'Config::ZOMG', '1.000000';

Then in order to install the dependencies on your development machine you can run `carton install`. This will install the dependencies in a folder called ‘local’ inside your source directory – that means your globally installed perl modules are untouched!
It’s worth noting that you won’t be able to run your application as you normally would (perl my_app.pl) because perl won’t know where to load the dependencies from. Instead, you will now have to prefix everything with carton exec, e.g. carton exec perl my_app.pl!

Version Control

Once you’ve ran carton install, you’ll notice a file, `cpanfile.snapshot` has been created. That does not only include your dependencies, but also the dependencies of these dependencies, at the correct versions. That means you don’t have to worry about writing down every single version or risking having the version of a dependency change in the future – everything is permanently recorded for you.
You will want to add this file to version control:

git add cpanfile cpanfile.snapshot

If your application ever needs a new dependency, all you have to do is tweak the cpanfile, re-run carton install and commit the changes to cpanfile.snapshot.
When another developer checks out the repository, they have to run carton install to get the dependencies, and use carton exec like you did.

Deployment

There are two ways of installing your dependencies: one is to get the dependencies from the Internet and the other to keep them locally.
If you want to do the former, your deployment process just has to pull the latest version of the repository and do:

carton install --deployment

(And of course run the application with carton exec).
If you don’t want to get the dependencies from the Internet, your deployment process will have to run carton bundle before deployment.

This will bundle your dependencies into the vendor/ folder. This folder will then have to be copied on to your production machine. Once there, you can run carton install --deployment --cached.

In conclusion, carton is an extremely useful and easy-to-use tool. The only part that was difficult for me was figuring out working versions of the dependencies, but that is something one has to do when writing a list of dependencies either way. I certainly recommend it to anyone wishing to use modern Perl deployment practices!

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.