Creating a Perl Module

From Organic Design
Jump to: navigation, search
Procedure.svg Creating a Perl Module
Organic Design procedure

A Perl module is essentially just a .pm file which has a specific structure allowing it to extend the native Perl runtime environment to add new namespaces, objects and subroutines etc. The .pm structure also allows for automatically generated documentation.

Create package skeleton

To ensure that the module is added to the operating system environment properly, it should be wrapped into a package so that it can be installed with make and make install in the usual way. We use the h2xs utility to create an initial functional package template.

h2xs -XAn OrganicDesign::Example

This will create a working structure which can be installed and then included in a Perl script with use OrganicDesign::Wiki.

Modify the module file

In the directory structure created by h2xs is a subdirectory called lib which contains a template .pm module file which can be refined and extended to your needs. Generally the first thing to do would be to add comments at the beginning describing the module, and edit the documentation section at the end. Then create a constructor and other required methods. See for an example of one of our modules.

Install the package

Change into the directory generated by h2xs, then install as usual:

cd OrganicDesign-Example
perl Makefile.PL
make install

To test that it has installed into the environment correctly, you can see if you can bring up its manual page:

man OrganicDesign::Example

Then create a test script to see if it can be used from within the Perl environment properly.

See also