One of the very best things about Perl is CPAN, a repository of modules to do everything from browse the web to manipulating image files. CPAN provides a consistent method for installing modules (install cpanminus and then
cpanm <Module::Name>) and the largest number of modules of all the scripting languages. More often than not, what you are trying to do has already been done and exists as a CPAN module. In the event you are doing something new, the best way to give back to the community and get free help is to encapsulate your work and distribute it as a module on CPAN.
Historically, creating a module suitable for general consumption was a confusing. Tutorials from years past abound, each one longer than the previous, and always employing a different toolchain, making synthesis of common concepts impossible. These days, however, things are much easier. In the past 6 years or so the Perl community has tried on a number of methods for building modules. The focus of this article will be a recent (circa late 2009…the Perl ecosystem takes a measured pace) build system, Dist::Zilla.
Whereas ExtUtils::MakeMaker and Module::Build are systems for building, testing, and installing a release, Dist::Zilla sits at a higher level. With Dist::Zilla, you create a single file that controls the build & test flow (using ExtUtils::MakeMaker under the hood) but also provide functionality for generating semi-boilerplate files (LICENSE, MANIFEST, META.yml) and releasing the code via CPAN. The configuration file,
dist.ini, is easy-to-read and short in contrast to prior build systems.
To start using Dist::Zilla, install it using the standard CPAN shell command (
cpan -i Dist::Zilla) or with cpanminus (
cpanm Dist::Zilla). You utilize Dist::Zilla through the command
dzil; if that’s not in your path (
which dzil), then you’ll want to find it and symlink it somewhere useful or add it to your path. Global setup of dzil is done by invoking
dzil setup and answering the questions it poses. With that done you can very easily mint a new distribution:
1 2 3 4
In the newly created Number-Cruncher directory you’ll find a
lib directory containing
Number/Cruncher.pm and a
1 2 3 4 5 6 7 8 9
At this point, code and tests are the only things needed to have a Perl module. Here’s some simple code for
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
And the associated test file that I created,
1 2 3 4 5 6 7 8 9 10 11 12
In three files—
t/number-cruncher.t—we have a module. Run the test with
dzil test and you’ll see Dist::Zilla build your module into a temporary directory and run the test suite from there:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
To build a distributable tarball, just run
1 2 3 4 5 6 7 8 9 10 11 12 13
If you already have a PAUSE account, you can use
dzil release to upload that tarball to PAUSE for inclusion in CPAN. If you haven’t authored a Perl module before, request an account for uploading modules to CPAN. With PAUSE and Dist::Zilla, creating widely-available Perl modules is easy.