Perl Module Basics
Modules
Modular programming requires two facilities in a programming language
encapsulation and interfaces. Packages provide encapsulation by furnishing
separate namespaces for subroutines and variables. Modules are Perl's facility for
providing packages with interfaces. In actuality, Perl's support for interfaces are a
set of conventions around the use of packages and module filenaming. There is no
module keyword
6
and no extra syntax to learn.
You tell Perl your module s name in two ways first, by naming the module file.
The filename corresponds to the module name by replacing the :: marks with file
system separators and appending a .pm extension to the end. For example, here are
some module names and their associated filenames on UNIX and Windows systems:
Table 2 1. Examples of Module Names Converted to Filenames on UNIX and
Windows systems
Module Name
UNIX Filename
Windows Filename
CGI
CGI.pm
CGI.pm
HTML::Template
HTML/Template.pm
HTML\Template.pm
Scalar::List::Utils
Scalar/List/Utils.pm
Scalar\List\Utils.pm
Secondly, at the top of your module file you declare the name in a package line:
package Hello;
sub greet {
my $name = shift;
print "Hello, $name!\n";
}
1;
TIP Perl modules must end by with a true statement. This tells Perl
that your module compiled successfully. Leaving off the true statement
will result in a compilation error.
6. At least not yet! Early Perl 6 designs include mention of a new keyword for modules and
classes separate from normal packages.
27
27
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved