Perl Programming/Code Reuse (Modules)

← Regular Expressions Reference | Objects →


OverviewEdit

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 http://cpan.org. 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 http://Search.cpan.org 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 moduleEdit

Find the perl module you want at http://cpan.org, 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 ModuleEdit

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:

1;

The following is a valid perl module:

package My::Module::Name;

1;

ExampleEdit

We create a new file called ExampleModule.pm, 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";
}

1;

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

perl -c ExampleModule.pm

It will print out "ExampleModule.pm 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:

#!/usr/bin/perl

use ExampleModule;

hello_world();

exit;

Voilá! You have made a perl module.

Create a CPAN-style Perl moduleEdit

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="myemail@gmail.com"

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
>make test
>sudo make install
Last modified on 1 December 2012, at 15:28