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.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What is CPAN?

    Answer

    A mirrored archive of Perl distributions with their documentation and tests

    CPAN is the Comprehensive Perl Archive Network: distributions, their POD and their test results, mirrored around the world.

  2. What does cpanm do when the module is already installed at the requested version?

    Answer

    It reports that the module is up to date and installs nothing

    cpanm checks the installed version and only builds when the requirement is not met, which is why a core module needs no work.

  3. Why would you install modules with --local-lib?

    Answer

    To keep them in your home directory without root and without touching the system library

    local::lib points the install at a private folder, and the eval line of the shell profile tells Perl where it is.

  4. What is a cpanfile?

    Answer

    A list of a project's dependencies that cpm and cpanm read

    The cpanfile records the modules and version constraints, and a tool installs them all from that one file.

  5. What does cpm install do better than one cpanm call per module?

    Answer

    It fetches the dependencies in parallel

    cpm reads the cpanfile and downloads in parallel, so a project with a long dependency list installs in one quick step.

  6. What does this program print? use JSON::PP; my $json = JSON::PP->new->canonical; print $json->encode({ name => "Ada" }), "\n";

    Answer

    {"name":"Ada"}

    The canonical form sorts the keys and writes no space after the colon, which is the compact JSON form.

  7. What does this program print? use JSON::PP; my $data = JSON::PP->new->decode('{"n": 2}'); print $data->{n} * 3, "\n";

    Answer

    6

    decode returns a hash reference, and the value under n is the number 2.

  8. What does this program print? print exists $INC{"JSON/PP.pm"} ? "loaded\n" : "missing\n"; use JSON::PP;

    Answer

    loaded

    A use statement is a BEGIN block, so the module is loaded while the file is compiled, before the first print runs.

  9. What does this program print? use Module::CoreList; my $first = Module::CoreList->first_release("JSON::PP"); print defined $first ? "in core since $first\n" : "not in core\n";

    Answer

    in core since 5.013009

    The release is the first Perl version that shipped the module, which the module records and not the version of the interpreter.

  10. A program stops with Can't locate Text/CSV.pm in @INC. What does it mean?

    Answer

    Perl found the module in no library folder, so it is not installed

    The message lists the folders Perl searched; installing the module with cpanm puts the file into one of them.

Article author: Arthur Isaev