CPAN and cpanm
What CPAN is
CPAN is the Comprehensive Perl Archive Network, a mirrored library of more than two hundred thousand modules. Almost every problem you meet in Perl has a tested module there, from CSV parsing to database access.
Search it at metacpan.org, where each distribution shows its documentation, test results and dependency list.
Install with cpanm
The classic CPAN shell is slow and interactive. cpanminus is a single script that installs a module and its dependencies without questions, and it is what most people use today.
curl -L https://cpanmin.us | perl - App::cpanminus
cpanm JSON::PP
JSON::PP is up to date. (4.16)
The second command installs a module that ships with Perl, so it reports that nothing needs doing. Try cpanm Path::Tiny to see a real download and build.
Keep modules local
Installing into the system library needs root and can conflict with modules the distribution owns. local::lib points the install at a folder in your home directory, and a shell variable tells Perl where to find it.
cpanm --local-lib=~/perl5 local::lib
eval $(perl -I ~/perl5/lib/perl5 -Mlocal::lib)
Add that eval line to your shell profile once, and every later cpanm call installs into the same private library. Many version managers such as perlbrew do this step for you.
cpanfile and cpm
A project should record its dependencies in a cpanfile, the same idea as a requirements file in Python. The file lists modules and version constraints in plain Perl syntax.
cpanfile
requires 'JSON::PP', '4.00';
requires 'Path::Tiny', '0.144';
on 'test' => sub {
requires 'Test::More';
};
cpm install reads that file and fetches everything in parallel, which is much faster than one cpanm call per module. A teammate then clones the project and runs one command.
Check what you have
Before installing anything, ask whether the module is already present. The -M flag with a version prints the installed version, and corelist says whether the module ships with Perl itself.
perl -MJSON::PP -e 'print $JSON::PP::VERSION, "\n"'
4.16
corelist Path::Tiny
Path::Tiny was not in CORE (or is not a module)
Core modules need no install and travel with every Perl, so prefer them when two modules solve the same problem equally well.
Next: Objects with bless
Article author: Arthur Isaev