Perl module: Difference between revisions

Content deleted Content added
corrected to_string function name
m Disambiguating links to Object-orientation (link changed to Object-oriented programming) using DisamAssist.
 
(76 intermediate revisions by 47 users not shown)
Line 1:
[[File:Perl_module.png|thumb|Diagram of the mechanism of using perl modules.]]
A '''Perl module''' is a discrete component of software for the [[Perl]] programming language. A module is distinguished by a unique namespace, e.g. "CGI" or "Net::FTP" or "XML::Parser". By conventions, there is typically one module per file with a .pm extension. A collection of one or more modules, with accompanying documentation and build scripts, compose a package. The Perl community has a sizable library of packages available for search and download via [[CPAN]].
A '''Perl module''' is a discrete component of [[software]] for the [[Perl]] [[programming language]]. Technically, it is a particular set of [[Convention (norm)|conventions]] for using [[#Perl packages and namespaces|Perl's package mechanism]] that has become universally adopted.{{discuss|Packages and modules}}
A module defines its [[source code]] to be in a ''package'' (much like a [[Java package]]), the Perl mechanism for defining [[namespaces]], e.g. ''CGI'' or ''Net::FTP'' or ''XML::Parser''; the file structure mirrors the [[namespace]] structure (e.g. the source code for ''Net::FTP'' is in ''Net/FTP.pm''). Furthermore, a module is the Perl equivalent of the [[class (computer science)|class]] when [[object-oriented programming]] is employed.{{discuss|Packages and modules}}
 
A collection of modules, with accompanying [[Software documentation|documentation]], [[Build automation|build scripts]], and usually a [[test suite]], composes a '''distribution'''. The Perl community has a sizable library of distributions available for search and download via [[CPAN]].
It is common for Perl modules to have embedded documentation in Perl's [[Plain Old Documentation]]
format. Many modules favor an [[object-oriented]] style, but many are [[procedural]] instead, especially old modules.
 
Perl is a language allowing many different styles of programming. A developer is as likely to find a module written in a [[Procedural programming|procedural]] style (for example, [https://metacpan.org/module/Test::Simple Test::Simple]) as [[Object-oriented programming|object-oriented]] (e.g. [https://metacpan.org/module/XML::Parser XML::Parser]), both are considered equally valid according to what the module needs to do. Modules might also be used to [[mixin]] methods ([https://metacpan.org/module/DBIx::Class DBIx::Class]) or be a [[compiler directive|pragma]] ([http://perldoc.perl.org/strict.html strict.pm]) which has an effect immediately upon being loaded. Modules can even be used to alter the syntax of the language. The effect of Perl modules are usually limited to the current [[scope (programming)|scope]] in which it was loaded.
Below is an example of a very simple object-oriented Perl module and a short program which makes use of the module. It is implemented in a dialect of Perl5 which is compatible with Perl 5.6.0 and higher.
 
It is common for Perl modules to have embedded documentation in Perl's [[Plain Old Documentation]] format. POD imposes little structure on the author. It is flexible enough to be used to write articles, web pages and even entire books such as [https://web.archive.org/web/20070316052422/http://www.oreilly.com/catalog/pperl3/colophon.html Programming Perl]. Contrast with [[javadoc]] which is specialized to documenting [[Java (programming language)|Java]] classes. By convention, module documentation typically follows the structure of a [[Manual page (Unix)|Unix man page]].
 
The language of Perl is defined by the single implementation (referred to as "perl") and is added to (and in rare occasions taken away from) each new release. For this reason it is important for a module author to be aware what features they're making use of and what the minimum required version of perl is. The code on this page requires perl 5.6.0 which is considered rather old by now.
 
== Examples ==
 
What follows are examples of "[[Hello world program|Hello, World]]" implemented in different styles of modules. It must be understood that a module is not necessary in Perl; functions and code can be defined and used anywhere. This is just for example purposes. Contrast with [[Java (programming language)|Java]] where a class is always necessary. A real "Hello, World" function would be written like so:
<syntaxhighlight lang="perl">
sub hello { "Hello, world!\n" }
print hello();
</syntaxhighlight>
or simply printed in one line:
<syntaxhighlight lang="perl">
print "Hello, world!\n";
</syntaxhighlight>
 
===Procedural example===
Here is "Hello, World" implemented as a procedural module with a customizable target for the greeting, just to make things interesting. Also included is a short script to illustrate the module's use.
 
====''hello_world.pl''====
 
<syntaxhighlight lang="perl">
#!/usr/bin/env perl
# Loads the module and imports any functions into our namespace
# (defaults to "main") exported by the module. Hello::World exports
# hello() by default. Exports can usually be controlled by the caller.
use Hello::World;
 
print hello(); # prints "Hello, world!\n"
print hello("Milky Way"); # prints "Hello, Milky Way!\n"
</syntaxhighlight>
 
====''Hello/World.pm''====
<syntaxhighlight lang="perl">
# "package" is the namespace where the module's functionality/data resides.
# It dictates the name of the file if you want it to be "use"d.
# If more than one word, it constrains the ___location of the module.
 
package Hello::World;
 
# By default Perl allows you to use variables without declaring
# them. This may be convenient for short scripts and one-liners.
# But in a longer unit of code such as a module it is wise to declare
# your variables both to catch typos and to constrain their
# accessibility appropriately from outside the module. The strict pragma
# forces you to declare your variables.
use strict;
# Similarly, Perl does not issue most compiler or run-time warnings by default.
# More complicated scripts, such as most modules, will usually find them very
# helpful for debugging. The warnings pragma turns on optional warnings.
use warnings;
# A module's version number is stored in $ModuleName::VERSION; certain
# forms of the "use" built-in depend on this variable being defined.
 
our $VERSION = '1.00';
 
# Inherit from the "Exporter" module which handles exporting functions.
# Most procedural modules make use of this.
 
use base 'Exporter';
 
# When the module is invoked, export, by default, the function "hello" into
# the namespace of the using code.
 
our @EXPORT = qw(hello);
 
# Lines starting with an equal sign indicate embedded POD
# documentation. POD sections end with an =cut directive, and can
# be intermixed almost freely with normal code.
 
=head1 NAME
 
Hello::World - An encapsulation of a common output message
 
=head1 SYNOPSIS
 
''helloworld.pl''
----
#!/usr/bin/perl -w
use Hello::World;
myprint $hello = Hello::World->new();
$hello->print hello("Milky Way");
 
=head1 DESCRIPTION
 
This is a procedural module which gives you the famous "Hello, world!"
message, and it’s even customizable!
 
=head2 Functions
 
The following functions are exported by default
 
=head3 hello
 
print hello();
print hello($target);
 
Returns the famous greeting. If a C<$target> is given it will be used,
otherwise "world" is the target of your greeting.
 
=cut
 
# define the function hello().
 
sub hello {
my $target = shift;
$target = 'world' unless defined $target;
return "Hello, $target!\n";
}
 
=head1 AUTHOR
 
Joe Hacker <joe@joehacker.org>
 
=cut
 
# A Perl module must end with a true value or else it is considered not to
# have loaded. By convention this value is usually 1 though it can be
# any true value. A module can end with false to indicate failure but
# this is rarely used and it would instead die() (exit with an error).
1;
</syntaxhighlight>
----
 
Since Hello/World.pm is not in your @INC path, you must specify . on the command line to run the above example:
''Hello/World.pm''
 
----
perl -I. hello_world.pl
# The 'package' command gives the name of the module or class.
 
===Object-oriented example===
package Hello::World;
Here's an example of the same thing done in an object-oriented
use strict;
style. The advantage of an OO module is that each object can be configured
use warnings;
independently from other objects.
 
# By convention, a module's version number is stored in
====''hello_world.pl''====
# $ModuleName::VERSION; certain forms of the "use" built-in depend
<syntaxhighlight lang="perl">
# on this variable being defined.
#!/usr/bin/env perl
 
our $VERSION = "1.0";
use Hello::World;
my $hello = Hello::World->new;
# Lines starting with an equal sign indicate embedded POD
$hello->print; # prints "Hello, world!\n"
# documentation. POD sections end with an =cut directive, and can
$hello->target("Milky Way");
# be intermixed almost freely with normal code.
$hello->print; # prints "Hello, Milky Way!\n"
 
=head1 NAME
my $greeting = Hello::World->new(target => "Pittsburgh");
$greeting->print; # prints "Hello, Pittsburgh!\n"
Hello::World - An encapsulation of a commonly output message
$hello->print; # still prints "Hello, Milky Way!\n"
</syntaxhighlight>
=head1 SYNOPSIS
 
use ====''Hello::/World;.pm''====
 
my $hw = new Hello::World();
<syntaxhighlight lang="perl">
$hw->print();
# In Perl there is no special 'class' definition. A namespace is a class.
package Hello::World;
=head1 DESCRIPTION
 
use strict;
This is an object-oriented library which can print the famous "H.W."
use warnings;
message.
our $VERSION = "1.00";
=head1 METHODS
 
=head1 NAME
=over
Hello::World - An encapsulation of a common output message
=item new
=head1 SYNOPSIS
Instantiates an object which holds a greeting message.
use Hello::World;
=cut
my $hello = Hello::World->new();
$hello->print;
sub new {
my $pkg = shift;
=head1 DESCRIPTION
my $self = bless({
message => "Hello, world!",
This is an object-oriented library which can print the famous "H.W."
}, $pkg);
message.
return $self;
}
=head2 Methods
=item to_string
=head3 new
Returns the greeting as a string
my $hello = Hello::World->new();
my $hello = Hello::World->new( target => $target );
=cut
Instantiates an object which holds a greeting message. If a C<$target> is
sub to_string {
given it is passed to C<< $hello->target >>.
my $self = shift;
return $self->{message};
=cut
}
# The constructor of an object is called new() by convention. Any
=item print
# method may construct an object and you can have as many as you like.
Outputs the greeting to STDOUT
sub new {
my($class, %args) = @_;
=cut
my $self = bless({}, $class);
sub print {
my $self = shift;
my $target = exists $args{target} ? $args{target} : "world";
print $self->to_string(),"\n";
$self->{target} = $target;
}
return $self;
=back
}
=head1 AUTHOR
=head3 target
Joe Hacker <joe@joehacker.org>
my $target = $hello->target;
=cut
$hello->target($target);
# The lone "1;" at the end of the file indicates that the module
Gets and sets the current target of our message.
# has been successfully initialized. Some modules contain code
# that runs as soon as the module is compiled, before the program
=cut
# that included it is finished compiling; they can arrange to return
# a non-true value if something goes wrong.
sub target {
1;
my $self = shift;
if ( @_ ) {
my $target = shift;
$self->{target} = $target;
}
 
return $self->{target};
}
=head3 to_string
my $greeting = $hello->to_string;
Returns the $greeting as a string
=cut
sub to_string {
my $self = shift;
return "Hello, $self->{target}!";
}
=head3 print
$hello->print;
Outputs the greeting to STDOUT
=cut
sub print {
my $self = shift;
print $self->to_string(), "\n";
}
=head1 AUTHOR
Joe Hacker <joe@joehacker.org>
=cut
1;
</syntaxhighlight>
----
 
==Perl packages and namespaces==
A running Perl program has a built-in [[namespace]] called "<code>main</code>", which is the default name. For example, a subroutine called <code>Sub1</code> can be called as <code>Sub1()</code> or <code>main::Sub1()</code>. With a variable the appropriate [[Sigil (computer programming)|sigil]] is placed in front of the namespace; so a scalar variable called <code>$var1</code> can also be referred to as <code>$main::var1</code>, or even <code>$::var1</code>. Other namespaces can be created at any time.
<syntaxhighlight lang="perl">
package Namespace1;
$var1 = 1; # created in namespace Namespace1, which is also created if not pre-existing
our $var2 = 2; # also created in that namespace; our required if use strict is applied
my $var3 = 3; # lexically-scoped my-declared - NOT in any namespace, not even main
</syntaxhighlight>
 
<syntaxhighlight lang="perl">
$Namespace2::var1 = 10; # created in namespace Namespace2, also created if not pre-existing
our $Namespace2::var2 = 20; # also created in that namespace
my $Namespace2::var3 = 30;#compilation error:my-declared variables CAN'T belong to a package
</syntaxhighlight>
 
Package declarations apply package scope till the next package declaration or the end of the block in which the declaration is made.
<syntaxhighlight lang="perl">
our $mainVar = 'a';
package Sp1;
our $sp1aVar = 'aa';
print "$main::mainVar\t$sp1aVar\n"; # note mainVar needs qualifying
package Sp2;
our $sp2aVar = 'aaa';
print "$main::mainVar\t$Sp1::sp1aVar\t$sp2aVar\n";# note mainVar and sp1aVar need qualifying
package main;
print "$mainVar\t$Sp1::sp1aVar\t$Sp2::sp2aVar\n"; # note sp1aVar and sp2aVar need qualifying
 
$mainVar = 'b';
{
# NOTE previously created packages and package variables still accessible
package Sp1;
our $sp1bVar = 'bb';
print "$main::mainVar\t$sp1aVar\t$sp1bVar\n"; # note mainVar needs qualifying
{
package Sp2;
our $sp2bVar = 'bbb';
print "$main::mainVar\t$Sp1::sp1aVar$Sp1::sp1bVar\t$sp2aVar$sp2bVar\n";
} # note mainVar and sp1...Var need qualifying
print "$main::mainVar\t$sp1bVar$sp1aVar\t$Sp2::sp2bVar$Sp2::sp2aVar\n";
} # note package Sp1 applies by default
# main applies again by default; all package variables still accessible as long as qualified
print "$mainVar\t$Sp1::sp1aVar$Sp2::sp2bVar\n";
</syntaxhighlight>
 
===Packages and modules===
Conventionally, namespaces are associated with modules; in practice, there is usually one namespace per module and vice versa, but that's not mandated by the language. For example, the 'standard' module CGI.pm has the following declaration at its top:
<syntaxhighlight lang="perl">
package CGI;
</syntaxhighlight>
This module, and its functionality, would commonly be invoked as follows:
<syntaxhighlight lang="perl">
use CGI (':standard'); # imports many functions, including b()
...
print b('Hello, world'); # outputs <b>Hello, world</b>
</syntaxhighlight>
A 'missing' subroutine ''could'' be added from the using program's namespace.
<syntaxhighlight lang="perl">
sub CGI::bi { # define target namespace (CGI) and sub name (bi)
return b(i($_[0]));
}
</syntaxhighlight>
and invoked as below:
<syntaxhighlight lang="perl">
print CGI::bi('Hello, world'); # outputs <b><i>Hello, world</i></b>
</syntaxhighlight>
However, though technically feasible, that would be dubious programming practice. You might just as well define the sub in the calling namespace, and call it from that namespace.
 
== Further reading ==
* [http://perldoc.perl.org/perlmod.html Perl modules (packages and symbol tables)]
* [http://perldoc.perl.org/perlmodlib.html Constructing new Perl modules and finding existing ones]
* [http://perldoc.perl.org/perlmodstyle.html Perl module style guide]
* [http://perldoc.perl.org/perlnewmod.html Preparing a new module for distribution]
* [http://www.togotutor.com/learn-perl/pm-perl.php Perl module configuration and installation]
* [[CPAN]]
 
{{Perl}}
 
{{DEFAULTSORT:Perl Module}}
[[Category:Modularity]]
[[Category:Perl]]