Writing Perl Modules
Kevin Eye
Buffalo Perl Mongers
Why write modules?
- reusable
- namespaces
- modular
- testable
Installing modules
- search.cpan.org
- cpan install Some::Module
- compiling
- perl Makefile.PL
- man perlmodinstall
- etc.
Using modules
- include path
- @INC
- perl -V
- use lib '/some/directory'
- PERL5LIB
- perl -MSome::Module -le 'print $Some::Module::VERSION'
Packages and the symbol table
- scope
- lexical (my operator)
- dynamic (local operator)
- global (symbol table)
- packages and the package operator
Including other code
- eval STRING
- do FILENAME
- like eval, but loads from a file
- searches @INC
- require FILENAME
- like do
- only runs each file once
- require MODULE
- like require FILENAME
- replaces :: with / in MODULE and adds .pm
Including other code
- use MODULE LIST
- like require MODULE
- runs at compile time
- calls MODULE::import(LIST)
- LIST is optional
- just like
BEGIN { require Module; import Module LIST; }
- no MODULE LIST
- like use, but calls MODULE::unimport(LIST)
Tips for writing modules
- use strict
- use warnings
- use Exporter (for non OOP modules)
- use Carp
Tools for writing modules
- h2xs -AX --skip-exporter --use-new-tests -n Foo::Bar
- module-starter --module=Foo::Bar
- Makefile.PL
- POD (plain old documentation)
- tests
- XS
- Inline::C, etc.
More information
- man perlmod
- man perlmodlib
- man perlmodstyle
- man perlmodinstall
- man perlnewmod
- man perlboot (beginner OOP tutorial)
- man perltoot (OOP tutorial)
- man perlobj
- man perlpod
- man perlxstut
- man perlxs
- Inline