Creating a Perl Module

From Organic Design wiki
Revision as of 11:25, 14 August 2008 by Sven (talk | contribs) (Templates added for creating perl modules including using Exporter for short names)
Procedure.svg Creating a Perl Module
Organic Design procedure

Package template

Creating a perl package requires a namespace to be specified, and an exit signal of 1 after reading a perl module.

<perl>
  1. !/usr/bin/perl

use strict; use warnings;

  1. Convention: Package namespace is capitalized

package MyModule;

  1. Some perl functions
  1. Library returns true when read in

1; </perl>

Exporting names

The Exporter package allows convenient short names with the tradeoff of polluting your namespace. See perldoc Exporter


{{{1}}}

Defining the path

The package needs to be located in the @INC path where a perl script can load and use it.

{{{1}}}

You can add a new path to @INC with;

<perl>

use lib "/full/path/to/file"; </perl>

inside perl scripts. However, this is hardcoded into the program itself. If you want to specify aditional @INC path information, they environment variable PERL5LIB can be used, e.g.

{{{1}}}


Cone.png We need to make a general package first which:
  • Collects a bunch of functions we use into one includable module
  • Makes the module installable as one of potentially many modules within OrganicDesign::
  • Adds the installation of it into the Debian Post Install
  • Ensure easy upgradability (maybe by packaging into a debian package), see Creating a Debian package

Example

<perl>


use OD::Wiki; $odwiki = OD::Wiki->new( url => 'http://www.organicdesign.co.nz', user => 'foo', pass => 'bar' );

$odwiki->edit( title => 'MyArticle', content => 'foo', summary => 'testing', minor => 1 ); </perl>

See also