Difference between revisions of "Creating a Perl Module"

From Organic Design wiki
(See also)
m
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{procedure
 
{{procedure
|description = This procedure defines the steps required to package up a collection of subroutines and other official packages into proper Perl modules which can be installed from a script with ''use OrganicDesign::Wiki'' for example. These modules can then be installed into the Perl environment as part of the [[Debian Post Install]].
+
|description = This procedure defines the steps required to package up a collection of subroutines and other official packages into proper Perl modules which can be installed from a script with ''use OrganicDesign::Wiki'' for example. These modules can then be installed into the Perl environment as part of the [[Debian Post Install]].
|role = dev
+
| role = dev
 +
| status = in use
 
}}
 
}}
 +
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.
  
We need to make a general package first which:
+
== Create package skeleton ==
*Collects a bunch of functions we use into one includable module
+
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 [http://linux.about.com/library/cmd/blcmdl1_h2xs.htm h2xs] utility to create an initial functional package template.
*Makes the module installable as one of potentially many modules within ''OrganicDesign::''
+
h2xs -XAn OrganicDesign::Example
*Adds the installation of it into the our Debian [[packages]]
+
This will create a working structure which can be installed and then included in a Perl script with ''use OrganicDesign::Wiki''.
  
== Example ==
+
== Modify the module file ==
<perl>
+
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 [[:OrganicDesign::Wiki.pm]] for an example of one of our modules.
use OD::Wiki;
 
$odwiki = OD::Wiki->new(
 
url  => 'http://www.organicdesign.co.nz',
 
user => 'foo',
 
pass => 'bar'
 
);
 
  
$odwiki->edit(
+
== Install the package ==
title  => 'MyArticle',
+
Change into the directory generated by ''h2xs'', then install as usual:
content => '[[foo]]',
+
cd OrganicDesign-Example
summary => 'testing',
+
perl Makefile.PL
minor  => 1
+
make
);
+
make install
</perl>
+
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==
+
== See also ==
;[[G:Creating+a+perl+module|Creating a perl package]]
+
*[http://www.perlmonks.org/index.pl?node_id=431702 José's Guide for creating Perl modules]
 
 
; Perldoc
 
*[http://perldoc.perl.org/perlmod.html Perlmod]
 
 
*[http://www.perl.com/lpt/a/995 Making Perl Reusable with Modules - users Module::starter]
 
*[http://www.perl.com/lpt/a/995 Making Perl Reusable with Modules - users Module::starter]
 
*[http://perldoc.perl.org/perlmod.html perlmod documentation]
 
*[http://perldoc.perl.org/perlmod.html perlmod documentation]
 
 
*[http://mathforum.org/~ken/perl_modules.html Creating (and Maintaining) Perl Modules]
 
*[http://mathforum.org/~ken/perl_modules.html Creating (and Maintaining) Perl Modules]
 
*[http://articles.techrepublic.com.com/5100-10878_11-1044686.html Creating your first Perl module]
 
*[http://articles.techrepublic.com.com/5100-10878_11-1044686.html Creating your first Perl module]
 
*[http://world.std.com/~swmcd/steven/perl/module_mechanics.html Perl Module Mechanics]
 
*[http://world.std.com/~swmcd/steven/perl/module_mechanics.html Perl Module Mechanics]
 
*[http://www.perlmonks.org/index.pl?node_id=431702 José's Guide for creating Perl modules]
 
*[http://www.perlmonks.org/index.pl?node_id=431702 José's Guide for creating Perl modules]
 +
*[http://www.vromans.org/johan/articles/makemaker.html Makemaker made easy]

Latest revision as of 08:51, 31 August 2008

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 OrganicDesign::Wiki.pm 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
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