a car parked beside airplane

One of Perl’s car­di­nal strengths is the depth and vari­ety of add-​on mod­ules to extend its capa­bil­i­ties, col­lect­ed for the past 26 years and count­ing on CPAN, the Comprehensive Perl Archive Network. Starting with ver­sion 5.004 in 1997, Perl has come pack­aged with a mod­ule (also called CPAN) and asso­ci­at­ed command-​line client for down­load­ing and installing from this ser­vice. Some devel­op­ers favor alter­na­tive tools such as CPANPLUS and its cpanp com­mand or cpan­mi­nus and its cpanm, or tools built on the lat­ter such as Carton and Carmel.

My favorite of these over the past sev­er­al years has been Shoichi Kaji’s cpm, main­ly because it’s blaz­ing­ly fast. As an exam­ple, the doc­u­men­ta­tion cites an instal­la­tion of Plack, the Perl web appli­ca­tion toolk­it, as tak­ing three times as long using cpanm ver­sus cpm. Both use the same Menlo core code but cpm achieves its speed by break­ing down depen­den­cies into indi­vid­ual streams, installing mod­ules in par­al­lel, and syn­chro­niz­ing the nec­es­sary work­er processes.

Shoichi’s pre­sen­ta­tion from The Perl Conference 2016 pro­vides a great summary:

Shoichi Kaji: Why a new CPAN client cpm’ is fast” (2016)

It’s very impor­tant to note that cpm is not a drop-​in replace­ment for the cpan or cpanm command-​line tools. Firstly, it uses the sub­com­mand install, e.g., cpm install Module::Name. Also, by default, it installs mod­ules into a sub­di­rec­to­ry named local/ as if you spec­i­fied cpanm --local-lib-contained local. You might want this if you’re set­ting up a Perl project with its non-​core depen­den­cies in a sep­a­rate loca­tion addressed by the local::lib mod­ule; oth­er­wise, you should use cpm install --global to install into a direc­to­ry in Perl’s @INC array. I tend to do the lat­ter when devel­op­ing, declar­ing my project’s depen­den­cies in a cpanfile.

Speaking of cpanfiles, like cpanm --installdeps cpm will use a cpanfile to dri­ve project depen­den­cy instal­la­tion. In fact, it defaults to look­ing for one if you don’t spec­i­fy indi­vid­ual mod­ules on the com­mand line and sup­ports the version-​controlled cpanfile.snapshot file intro­duced by Carton for track­ing exact depen­den­cies used by your project. This is great for repeat­ed­ly build­ing Docker con­tain­ers and cpm makes that process even faster.

Although speed is its most impor­tant fea­ture, cpm has a cou­ple more tricks up its sleeve like installing from a Git repos­i­to­ry or self-​hosted DarkPAN.” Check out its includ­ed tuto­r­i­al.

One thought on “Fast Perl module installation with cpm

Leave a Comment

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