.pm
file.
For example,
h2xs -X -n Foo::Barwill create
Bar.pm
like this
package Foo::Bar; use 5.008008; use strict; use warnings; require Exporter; use AutoLoader qw(AUTOLOAD); our @ISA = qw(Exporter); # Items to export into callers namespace by default. Note: do not export # names by default without a very good reason. Use EXPORT_OK instead. # Do not simply export all your public functions/methods/constants. # This allows declaration use Foo::Bar ':all'; # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK # will save memory. our %EXPORT_TAGS = ( 'all' => [ qw( ) ] ); our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); our @EXPORT = qw( ); our $VERSION = '0.01'; # Preloaded methods go here. # Autoload methods go after =cut, and are processed by the autosplit program. 1; __END__
This page describes the initial contents of the .pm
file,
and explains how to use it as a starting point for a module.
package Foo::Bar; |
The package statement sets the default package for
global names:
variables, filehandles and subroutines.
Since a module typically holds code for a package,
it is convenient to set the default package name.
|
use 5.008008; |
This line causes a compile-time error if the version of the Perl interpreter
is less than 5.8.8.
Perl has evolved over the years. If you know that your module relies on features or bug fixes that are only available in a certain version of the interpreter, you can use a line like this to prevent it from running on any earlier version.
Short of that, you can write |
use strict; |
use strict enforces a restricted programming model on your code.
It is strongly recommended.
The most obvious effect of |
use warnings; |
use warnings turns on warnings for many questionable or potentially
erroneous coding practices, such as use of uninitalized variables.
Conventional wisdom holds that all Perl programs should run with warnings enabled.
However, I find |
require Exporter; |
Packages allow a program to be partitioned into separate namespaces.
However,
this leads immediately to the problem of how one package may access
the facilities of another.
Conceptually,
there are two possibilities:
Reaching downPerl does not enforce access restrictions between packages. Any line of code, in any package, can access variables and subroutines in another package simply by naming them:$circumference = 2 * $Math::PI * $radius; $area = Math::area($length, $width);There are at least two problems with this:
Pushing upAlternately, a package can place its own variables in the namespace of the caller's package:package Math; $main::PI = 3.1415926; sub main::area { ... }The problem with this is that it risks the very namespace collisions that packages were designed to prevent. The Perl5 FixPerl5 provides support for both reaching down and pushing up. An object-oriented interface allows code to reach down into a package namespace without violating encapsulation
Conversely, the |
use AutoLoader qw(AUTOLOAD); |
One of the design objectives of Perl5 is to allow the language to scale.
Current implementations compile a program each time it is run.
This is acceptable for a 1000 line program,
but could become a problem as programs scale up into the 10K-100K line range.
The
|
our @ISA = qw(Exporter); |
The @ISA array controls method inheritance in Perl modules.
If a method isn't found in the current package,
then Perl searches for it in the packages named in the
@ISA array.
(recursively, depth first).
|
# Items to export into callers namespace by de # names by default without a very good reason. # Do not simply export all your public functio # This allows declaration use Foo::Bar ' # If you do not need this, moving things direc # will save memory. our %EXPORT_TAGS = ( 'all' => [ qw( ) ] ); our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); our @EXPORT = qw( ); |
The usual way to include a module in your program is with a use
statement:
use Foo::Bar LISTThe Perl manual documents this statement as being exactly equivalent to BEGIN { require Foo::Bar; import Foo::Bar LIST }The require statement causes the file Foo/Bar.pm to be located,
read and compiled.
The
The
The
The If you're not exporting anything, you can delete these three statements. Delete the explanatory comments, too. |
our $VERSION = '0.01'; |
This assigns a version number to the module.
If a user writes
use Foo::Bar 1.01then the program will die if $VERSION is lower than
the value specified in the use statement.
It's a good idea to bump the version number of a module each time you change it. |
# Preloaded methods go here. |
This is where you put methods, subroutines, variables, and whatever
else the module needs.
Preloaded means that they will be compiled in the usual fashion:
at compile time.
Go ahead and delete the comment. |
# Autoload methods go after =cut, and are proc |
Methods that are to be compiled at run time via the Autoload
module are placed after the logical end of program text.
I've never had occasion to do this.
Delete this comment, too. |
1; |
A use statement eval s the contents of a file.
The last expression in the file must evaluate to true,
or the eval die s.
It is conventional to end
I like to drop the semicolon after the |
__END__ |
__END__ is the logical end of program text.
You can put anything you want after this token;
it will be ignored by the compiler.
It is common practice to put documentation after this token, in POD format. See Program PODs for details. |
use
a module that neither has nor
inherits an import
method,
and the interpreter will quietly ignore the problem.