Difference between revisions of "Creating a Perl Module"
(→See also) |
(Templates added for creating perl modules including using Exporter for short names) |
||
Line 4: | Line 4: | ||
}} | }} | ||
+ | ==Package template== | ||
+ | Creating a perl package requires a namespace to be specified, and an exit signal of 1 after reading a perl module. | ||
+ | {{code|<perl> | ||
+ | #!/usr/bin/perl | ||
+ | |||
+ | use strict; | ||
+ | use warnings; | ||
+ | |||
+ | # Convention: Package namespace is capitalized | ||
+ | package MyModule; | ||
+ | # Some perl functions | ||
+ | |||
+ | # 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 <code>perldoc Exporter</code> | ||
+ | |||
+ | {{code|<perl> | ||
+ | #!/usr/bin/perl | ||
+ | |||
+ | use strict; | ||
+ | use warnings; | ||
+ | |||
+ | # Convention: Package namespace is capitalized | ||
+ | package MyModule; | ||
+ | require Exporter; | ||
+ | @ISA = qw(Exporter); | ||
+ | |||
+ | # Provide a list of function names to export | ||
+ | @EXPORT_OK = qw(...); | ||
+ | |||
+ | # Some perl functions | ||
+ | |||
+ | # Library returns true when read in | ||
+ | 1; | ||
+ | </perl>}} | ||
+ | ===Defining the path=== | ||
+ | The package needs to be located in the @INC path where a perl script can load and use it. | ||
+ | {{code|<perl> | ||
+ | perl -e 'BEGIN{$"="\n"}; print "@INC\n"'; | ||
+ | /etc/perl | ||
+ | /usr/local/lib/perl/5.8.8 | ||
+ | /usr/local/share/perl/5.8.8 | ||
+ | /usr/lib/perl5 | ||
+ | /usr/share/perl5 | ||
+ | /usr/lib/perl/5.8 | ||
+ | /usr/share/perl/5.8 | ||
+ | /usr/local/lib/site_perl | ||
+ | . # <-- Current working directory | ||
+ | |||
+ | </perl>}} | ||
+ | You can add a new path to @INC with; | ||
+ | {{code|<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. | ||
+ | {{code|<perl> | ||
+ | # Direct assignment | ||
+ | setenv PERL5LIB /my/perl/modules | ||
+ | |||
+ | # Appending path | ||
+ | PERL5LIB = $PERL5LIB:/my/perl/modules | ||
+ | </perl>}} | ||
+ | |||
+ | {{todo| | ||
We need to make a general package first which: | We need to make a general package first which: | ||
*Collects a bunch of functions we use into one includable module | *Collects a bunch of functions we use into one includable module | ||
*Makes the module installable as one of potentially many modules within ''OrganicDesign::'' | *Makes the module installable as one of potentially many modules within ''OrganicDesign::'' | ||
− | *Adds the installation of it into the | + | *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 == | == Example == | ||
<perl> | <perl> | ||
+ | |||
+ | |||
use OD::Wiki; | use OD::Wiki; | ||
$odwiki = OD::Wiki->new( | $odwiki = OD::Wiki->new( | ||
Line 27: | Line 97: | ||
==See also== | ==See also== | ||
− | + | *[http://www.perlmonks.org/index.pl?node_id=431702 José's Guide for creating Perl modules] | |
− | |||
− | |||
− | *[http:// | ||
*[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] |
Revision as of 11:25, 14 August 2008
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.
Exporting names
The Exporter package allows convenient short names with the tradeoff of polluting your namespace. See perldoc Exporter
Defining the path
The package needs to be located in the @INC path where a perl script can load and use it.
You can add a new path to @INC with;
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.
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>