Jump to content

Perl Programming/Code reuse (modules)

From Wikibooks, open books for an open world
Previous: Regular expressions reference Index Next: Objects

Overview

[edit | edit source]

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. Functional
  2. Object-oriented

Some perl modules use both approaches.

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();

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.

How to install a Perl module

[edit | edit source]

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 change into this 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 | edit source]

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;

Example

[edit | edit source]

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 module

[edit | edit source]

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

How to pass parameters to a function in a Perl module

[edit | edit source]

When calling a function from a Perl module, the module name, function name and opening and closing parenthesis are used. If some parameters are to be passed, this is done in due order inside the parenthesis. The code inside the module gets the parameters the following way:

package functions;

sub count() {
  my ($command_type, $rc) = @_;

  []

After the first line, the function count can use the passed parameters $command_type and $rc. The main program calls the count() procedure as follows:

use 
sub count() {
  use functions;

  my $rc;

  $rc = [];

  []
  &functions:count("INSERT", $rc);
  []
Previous: Regular expressions reference Index Next: Objects