This is not a reference manual. Rather, it is a running account of how to do these things. More to the point, it is an account of how I do these things. Accordingly
Components of a module name are separated by double colons (::). Thus, we can have
Module | Function |
---|---|
Math::Complex | Complex number data type |
Math::Approx | Approximate x,y-values by a function |
String::Approx | Approximate string matching and substitution |
String::BitCount | Count number of "1" bits in strings |
without conflict.
One-component module names are a limited resource, and should only be taken for patently global things:
Module | Function |
---|---|
Config | Stores details of the Perl build configuration |
Names with three or more components risk confusing users:
Will the real module please stand up?
Math::Complex
might be a subclass of Math
,
or it might not.
It might be a subclass of String::Approx
, for that matter.
Module | Is Stored In |
---|---|
Config | Config.pm |
Math::Complex | Math/Complex.pm |
String::Approx | String/Approx.pm |
All module files have an extension of .pm
.
pm stands for Perl Module.
You may also encounter .pl
files.
pl stands for Perl Library.
.pl
files were used in Perl4;
.pm
files are preferred in Perl5.
@INC
.
INC is short for "include".
To see the initial contents of @INC
, do
>perl -V ... @INC: /usr/local/lib/perl5/5.8.6/i686-linux /usr/local/lib/perl5/5.8.6 /usr/local/lib/perl5/site_perl/5.8.6/i686-linux /usr/local/lib/perl5/site_perl/5.8.6 /usr/local/lib/perl5/site_perl/5.8.5/i686-linux /usr/local/lib/perl5/site_perl/5.8.5 /usr/local/lib/perl5/site_perl .As installed on my machine, the Perl interpreter looks for modules in eight places. Three of these have
i686-linux
in their path;
these are for modules that link to separately compiled C code,
and are therefore architecture dependent.
The last is the current working directory.
/usr/local/lib/perl5/5.8.6
contains modules that come
with the standard Perl5 distribution.
/usr/local/lib/perl5/5.8.5
contains modules that came with
the previous version of Perl. Modules from successive
distributions are kept in different directories to support programs
that may rely on older versions.
Finally, /usr/local/lib/perl5/site_perl/
contains modules
that are built and installed locally; i.e. are not part of the
standard distribution. Local and standard modules are separated in
this way so that a new release of the interpreter can replace the
standard modules without disturbing the locally installed ones.
So, if the modules listed above were installed on my system, they would be in
Module | Path |
---|---|
Config | /usr/local/lib/perl5/site_perl/5.8.6/Config.pm |
Math::Complex | /usr/local/lib/perl5/site_perl/5.8.6/Math/Complex.pm |
String::Approx | /usr/local/lib/perl5/site_perl/5.8.6/String/Approx.pm |
.pm
file,
and the interpreter executes it.
This means that you can edit the .pm
files in /usr/local/lib/perl5/site_perl/
,
and your edits will be effective immediately.
Don't.
Many people may be running programs that depend on the modules in
/usr/local/lib/perl5/site_perl
.
When the modules break, the programs break,
and then there are users lined up at your door.
/usr/local/src
,
it could be in your home directory,
or it could be somewhere else.
It should not be in
/usr/local/lib/perl5/site_perl/
.
I'll write .../development/
to refer to the development
directory, where ever it is.
.pm
file from the
development area to /usr/local/lib/perl5
.
For ordinary Perl modules, this is true. However. Some Perl modules link to dynamically loaded shared C libraries, via a special interface language called XS. This not only requires compiling and linking, but potentially involves all the worst kinds of system dependencies.
h2xs
.
h2xs
reads the .h
file for a C library
and creates a skeleton for the .xs
file that will be
required to build a Perl module that links to that library.
It also constructs a fairly elaborate system of directories and
makefiles in which to build and test the Perl module.
Over time, the structure that h2xs
creates has become the
standard for building Perl modules—even modules that don't link to C
code. I use this structure, because
.../development>h2xs -X -n Foo::Bar Writing Foo-Bar/lib/Foo/Bar.pm Writing Foo-Bar/Makefile.PL Writing Foo-Bar/README Writing Foo-Bar/t/Foo-Bar.t Writing Foo-Bar/Changes Writing Foo-Bar/MANIFEST .../development>The
-X
switch specifies that this module does
not link to C code.
The -n
switch gives the name of the new module.
h2xs
creates
Foo-Bar/
and populates it with the six files listed.
Foo-Bar/
is called
the module directory or
the build directory.
Let's look at the six files.
MANIFEST
MANIFEST
lists the files in the build directory.
MakeMaker
checks that all the files listed in MANIFEST
are present,
and complains if they aren't.
This is mainly used to ensure that module distributions are received intact.
README
README
contains a skeleton for a README file.
Creating a README file is optional,
but considered good practice.
Modules that are uploaded to CPAN are required to have README
files.
The README
file also contains this boilerplate
Copyright (C) 2005 your name here This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available.Perl is distributed under the terms of the GPL and the Artistic License; if these aren't the terms that you want for your module, you need to change this language.
Changes
h2xs
creates it, but it's up to the programmer to
maintain it.
t/Foo-Bar.t
t/
is a subdirectory for test files, and
t/Foo-Bar.t
contains a skeleton test routine for the module.
As created,
all it does is verify that the module can be loaded into a Perl program.
If nothing else,
this will catch syntax errors in the module.
Programmers are encouraged to create functional tests for their modules.
Makefile.PL
h2xs
creates Makefile.PL.
Makefile.PL.
contains a short Perl program:
.../development/Foo-Bar>cat Makefile.PL use 5.008006; use ExtUtils::MakeMaker; # See lib/ExtUtils/MakeMaker.pm for details of how to influence # the contents of the Makefile that is written. WriteMakefile( NAME => 'Foo::Bar', VERSION_FROM => 'lib/Foo/Bar.pm', # finds $VERSION PREREQ_PM => {}, # e.g., Module::Name => 1.1 ($] >= 5.005 ? ## Add these new keywords supported since 5.005 (ABSTRACT_FROM => 'lib/Foo/Bar.pm', # retrieve abstract from module AUTHOR => 'your name <your email address>') : ()), );The crucial line is the second one:
use ExtUtils::MakeMaker;
.
MakeMaker is the entry point to a large system of Perl modules.
These modules know how to write makefiles,
and various subclasses of them know how to write makefiles
for your particular system.
The WriteMakefile()
call causes an appropriate
makefile to be written.
Arguments to WriteMakefile()
control the contents
of the makefile.
lib/Foo/Bar.pm
lib/
.
As created, Bar.pm
contains some skeleton code and documentation;
see Module Anatomy
for a description of this.
N.B. Bar.pm
is created with same copyright notice as
README
.
perl Makefile.PL make make test make install
Makefile.PL
to create the makefile.
Makefile.PL
is a Perl program,
but it has no #!
line,
so you have to invoke the interpreter explicitly.
blib
(for Build LIB)
in the module directory,
and installs the module in it.
blib
is a private test area for the module. Try doing
.../development/Foo-Bar>ls -R blib/Pretty amazing, no? If
Foo::Bar
linked to C code,
you'd see those subdirectories populated with
.ix
files and
.al
files and
.bs
files and
.so
files and maybe a griffin or two.
A copy of your .pm
file is down there too: it's at
.../development/Foo-Bar/blib/lib/Foo/Bar.pm
t/Foo-Bar.t
,
with special command line switches that cause Perl to
find the copy of your module that is buried under blib/
,
and not any other copies that might be
floating around.
This is where you'll see syntax errors flagged,
and functional errors if you added your own tests.
/usr/local/lib/perl5/site_perl
.
On Unix systems, you typically need root access to do this.
As soon as it's installed, it's available to everyone on your system.
.../development/Foo-Bar>make distThis locates all the files listed in
MANIFEST
, tar's and
compresses them, and writes the archive to (for example)
Foo-Bar-1.00.tar.gzin the development directory. This archive is a complete distribution of your module. You can give it to anyone, and they can build and install your module on their own system.
blib/
would be complicated,
so the makefile creates a zero-length file named
pm_to_blib
in the module directory
and tracks its modification time instead.
If pm_to_blib
gets out of sync
with your .pm
files,
then the makefile will think blib/
is up to date and
won't rebuild it.
If this happens,
just delete pm_to_blib
and run make
again.
/usr/local/lib/perl5/site_perl
is owned
by root with 0755 permissions. This means that you must
su
to root in order to install a module there. If
you lack root on your system, you can
First, select a directory to hold your Perl modules and scripts. It doesn't strictly matter where the directory is located, but it must be somewhere that
/usr/tmp
is probably not.
We'll write
/home/uidfor your home directory.
Then go back to the development directory and type
.../development/Foo-Bar>perl Makefile.PL INSTALL_BASE=/home/uid .../development/Foo-Bar>make installThis will build the module and install it under your private directory, as follows:
/home/uid/lib/perl5/ # modules /home/uid/bin/ # scripts /home/uid/man/ # man pages
PERL5LIB
PERL5LIB
environment variable. In Bourne shells, run
export PERL5LIB=/home/uid/lib/perl5and Perl will add your directory to the front of
@INC
.
.../development>h2xs -X -n Foo::Bar .../development>h2xs -X -n Foo::ZotOne problem with this is that you have to keep hopping between directories to edit and build them. A bigger problem is that you can't test them together.
Suppose you make edits to both modules.
Now you try to test Foo::Bar
.
Foo::Bar
uses Foo::Zot
.
In particular,
it uses the currently released version,
which doesn't have the edits that you just made to Foo::Zot
in the development area.
So you can't test Foo::Bar
until you've installed
the new version of Foo::Zot
,
which you probably don't want to do right now,
since you haven't tested it yet.
The solution is to create both modules into the same directory:
.../development>h2xs -X -n Foo::Bar .../development>cp Foo-Bar/lib/Foo/Bar.pm Foo-Bar/lib/Foo/Zot.pmYou don't even have to mess with
Makefile.PL
to do this;
MakeMaker scans the build directory for .pm
files and includes them all in the makefile.
.../development/Foo-Bar>make testtests your module by running the test files in
t/
.
h2xs
creates t/Foo-Bar.t
for you,
but you can have as many .t
files as you like.
For example, if you are maintaining several .pm
files in the same module directory,
each one can have it's own .t
file:
Foo-Bar/t/Foo-Bar.t Foo-Bar/t/Foo-Zot.tTest files run in alphabetical order, so if you want to control the execution order, you can give them names like
Foo/Bar/t/1_basic.t Foo/Bar/t/2_advanced.t
make test
uses Test::Harness
to run the
test files.
Test files print their results on STDOUT.
Test::Harness
collects the output and reports the
number of tests that pass and fail.
The TAP
(Test Anything Protocol) documentation
details the format of the output expected from test files.
However, to get started, you only need to know a few things
1..Nwhere N is the total number of tests in the file. The skeleton provided in
Foo-Bar.t
has code to print this line.
ok n descriptionon success or
not ok n descriptionon failure. n is the test number; these increment from 1 through N as the tests run. The description is optional; it can be any text.
Test::Simple
and Test::More
modules
provide additional facilities to aid in writing tests.
I like to put
my $N = 1; sub Not { print "not " } sub OK { print "ok ", $N++, "\n" }at the top of my
.t
files.
Then I can write test code like
$actual eq $expected or Not; OK;
t/
is a convenient place to put data files for test
routines to use:
.../development/Foo-Bar/t/Foo-Bar.t .../development/Foo-Bar/t/fodder
make test
will run Foo-Bar.t
as usual;
it will ignore fodder
,
because that file doesn't have a .t
extension.
Foo-Bar.t
can then test Foo::Bar
on the
fodder
file.
Beware that when make test executes a .t
file,
the current working directory is the module directory, not t/
.
So
open FILE, "fodder"won't work. Instead, you have to write
open FILE, "t/fodder"
make TEST_VERBOSE=1 testThis will dump STDOUT from the test files to your screen. Some well chosen description text in the test output will make this more informative.
make test
runs all the .t
files in the t
directory. If you are only interested in
a single test file, you can run it with
make TEST_FILES=t/Foo-Bar.t testSwitches to
make test
can be combined. To see the
output from a single test file, do
make TEST_FILES=t/Foo-Bar.t TEST_VERBOSE=1 test
.pm
file under the module directory.
This used to require playing complicated games with @INC
or use lib
,
but as of Perl 5.004, there's a better way: blib
.
blib
is (what else?) a Perl module.
If you are in the module directory, you can do
.../development/Foo-Bar>perl -Mblib program.plto give
program.pl
access to Foo::Bar
.
If you are somewhere else,
give the path to the module directory as an argument to blib
somewhere/else>perl -Mblib=.../development/Foo-Bar program.pl
pod2text
,
pod2man
,
pod2html
,
pod2latex
,
pod2usage
.
Programmers are strongly encouraged to write PODs for all their Perl programs and modules. See module PODs for details.
man/man3/
.
The build procedure for a Perl module automatically
creates and installs a man page for the module.
make
invokes pod2man
to generate Foo::Bar.3
from Bar.pm
,
and make install
copies Foo::Bar.3
to the man3
directory.
@LIB
,
but...um...it isn't.
/usr/local/lib/perl5/site_perl/
Makefile.PL