Creating Perl modules
What is this about?
• Perl modules are a set of related functions in a file.
• They’re designed to be reusable by other modules or programs.
• Therefore useful to you:
• Collect useful functions in one place, stop copying and pasting code
• And useful to others:
• You too can be a CPAN author!
• There are many many ways to create modules; this is how I do it.
• Dist::Zilla makes it really easy.
• If you want to do it a different way go ahead – TIMTOWTDI
How easy is this?
• One-time setup:
cpan -i Dist::Zilla or ppm install Dist-Zilla
installs many (160) prerequisites
• Make a new module:
dzil new My::Module
What does Dist::Zilla do?
• Builds scaffolding and boilerplate for a CPAN-ready Perl module
• README, LICENSE, MANIFEST, META.yml and/or META.json,
Makefile.PL or Build.PL, lib/, bin/, t/ …
• Manages the building, testing, installation and release of distributions
• Extensible through plugins and plugin bundles to handle other repetitive
• Version numbers
• Saves you (the developer) time and stress
What doesn’t Dist::Zilla do?
• It’s not a system for users to install released or published code.
• It doesn’t make you write object (or any other kind of) oriented code.
• It doesn’t make you change existing code if you don’t want to.
Put your modules in lib/
• In lib/My/Module.pm:
# your code goes here
• Scripts go in bin/ if you have them.
The dist.ini file
name = My-Module
author = Mark Gardner <email@example.com>
license = Perl_5
copyright_holder = Aria Systems, Inc.
copyright_year = 2013
version = 0.001
The Basic plugin bundle
• Does not change your code
• Generates MANIFEST, Makefile.PL, META.yml, README
• Allows easy release to CPAN
• If you want to do more, add/change plugins in dist.ini
• perldoc Dist::Zilla::PluginBundle::Basic for more
Handling prerequisites in dist.ini
Sub::Exporter = 0.979
Acme::ProgressBar = 0
• Use both if AutoPrereqs doesn’t do what you want.
Handling documentation with
• In dist.ini:
• Makes Pod sections for
NAME, VERSION, AUTHOR, COPYRIGHT
• Gathers things like methods into a
• In your code:
# ABSTRACT: my cool module
# your code goes here
Managing version numbers (several
• Putting them in the right places
• [PkgVersion] and [OurPkgVersion]
• Automatically manage numbering
• prove -lr t
• Runs all tests in t/ using libraries in lib/
• Does not take advantage of Dist::Zilla changes to your code
• But it’s fast!
• dzil test
• Builds a temporary copy and runs its tests
• Should do exactly what end-user testing will do
• Can be slower since it’s building the distribution first
Releasing to CPAN
• PAUSE credentials in either ~/.pause or ~/.dzil/config.ini
• For trial (not for automatic update) releases:
dzil release --trial
Many many more techniques and plugins
• Custom profiles for multiple dist.ini and directory layouts
• Pod::Weaver plugins for custom sections, encodings, etc.
• Integration with Git, GitHub, Twitter, TravisCI, etc.
• Automatic generation of web services clients
• Building for other package managers like RPM amd Dpkg
• Commands and tests for Perl::Tidy, Devel::Cover, Perl::Critic, etc.
• Dist::Zilla users often become Dist::Zilla extenders