Perl Programming/Code Reuse (Modules)

From Wikibooks, open books for an open world
< Perl Programming
Jump to: navigation, search

← Regular Expressions Reference | Objects →


Perl modules (Files that end with the pm extension) are files of perl code that can be reused from program to program. There is an online repository of perl modules called CPAN (Comprehensive Perl Archive Network) at Many of these modules come standard with Perl, but others must be installed as needed.

There are thousands of perl modules that do everything from creating a temporary file to calling Amazon web services. These modules can make it easy to quickly write your application if you know how to find, install, and use the appropriate Perl modules. If you are thinking of writing your own Perl module, the best thing to do is to first search at to make sure you are not about to reinvent the wheel.

There are two major styles of Perl modules:

  1. Object-Oriented
  2. Functional

Some perl modules use both approaches.

To use an object-oriented Perl module you would do something like this:

use Foo;
my $foo = Foo->new();
print $foo->bar;  #call Foo's bar method and print the output.

A functional perl module might get used like this:

use Foo qw/bar/; # Import the name of the subroutine you want to use.
print bar();

How to install a Perl module[edit]

Find the perl module you want at, and download the gzipped file. Untar and unzip the file:

tar -zxvf MyModule.tgz

Then cd into the directory, and follow the instructions in the README or INSTALL file.

You can also use a command-line program called cpan, if you have it installed:

sudo cpan -imt Module::I::Want

To Write your own Perl Module[edit]

Perl modules differ from perl scripts in two key and simple ways. Instead of starting the module with "#!/path/to/perl", you start the file with the following:

package My::Module::Name;

You need to end the module with a true value, so the common practice is to do this at the end of the file:


The following is a valid perl module:

package My::Module::Name;



We create a new file called, and in it have the following code:

package ExampleModule;
use strict;
use base "Exporter";
our @EXPORT = qw/hello_world/;

sub hello_world
   print "hello, world!\n";


We can test to see if the syntax is valid by running:

perl -c

It will print out " syntax OK" if all is well. Otherwise, you can debug using the messages that are printed out.

Now we can use it in a script to see if it works:


use ExampleModule;



Voilá! You have made a perl module.

Create a CPAN-style Perl module[edit]

CPAN-style modules have test suites and a way to build the module into the perl library.

Download and install: Module::Starter from CPAN. Once this is installed, there will be a program called module-starter in your path. To create a new module, do the following from the command line:

module-starter --module=My::Module::Name, My::Other::Module::Name, --author="My Name" --email=""

It will then create a set of directories for you, including some shell module files with starter POD documentation. The perl modules will be inside the lib directory inside the directory that is created. These are the files to edit. You can put your tests for the modules into the "t" directory. To install and build the module, you do the following:

>perl Makefile.PL
>make test
>sudo make install