[ Perl tips index]
[ Subscribe to Perl tips ]
A module is a separate file containing Perl source code, which is loaded and executed at compile time. This means that when you write:
use CGI;
Perl looks for a file called CGI.pm (.pm for
Perl Modules), and upon finding it, loads it
in and executes the code inside it, before looking at the rest
of your program.
Sometimes you might choose to pass extra information to the module when you load it. Often this is to request the module create new subroutines in your namespace.
use CGI qw(:standard);
use File::Copy qw(copy);
Note the use of qw(), this is a list of words (in our case, just a single
word). It's possible to pass many options to a module when you load it. In
the case above, we're asking the CGI module for the :standard bundle of
functions, and the File::Copy module for just the copy subroutine.
Perl modules can do just about anything. In general, however, there are three main uses for modules:
use strict) or to allow you to write in other
languages, such as Latin (use Lingua::Romana::Perligata), or to provide new
language features (use Switch).
use Carp or
use CGI qw/:standard/).
use HTML::Template or
use Finance::Quote) for object oriented
programming.
Sometimes the boundaries are a little blurred. For example, the
CGI module provides both a class and the
option of extra subroutines, depending upon how you load it.
Perl searches through a list of directories that are determined
when the Perl interpretor is compiled. You can see this list
(and all the other options Perl was compiled with), by using
perl -V.
The list of directories which Perl searches for modules is stored
in the special variable @INC. It's possible
to change @INC so that Perl will search in other
directories as well. This is important if you have installed your
own private copy of some modules.
Of course, being Perl, there's more than one way to change
@INC. Here are some of the ways to add to
the list of directories inside @INC:
-I command-line switch with the location
of the extra directory to search. For example:
perl -I/path/to/libs
This can be done either in the shebang line, or on the command-line.
lib pragma in your script to inform Perl of extra directories. For
example:
use lib "/path/to/libs";
PERL5LIB environment variable with a colon-separated list of
directories to search. Note that if your script is running with taint checks
this environment variable is ignored.
PERL5LIB=/path/to/libs:/other/path/to/libs
Since use statements occur before regular Perl code is executed, modifying
@INC directly usually does not have the desired effect.
To find out which modules were included standard with your Perl installation type:
perldoc perlmodlib
To find out which modules have since been installed type:
perldoc perllocal
This is not fool-proof as many operating system packaging systems do not update the file used to provide this information. Further, modules installed by users without permission to update this file will also be missing.
[ Perl tips index ]
[ Subscribe to Perl tips ]
| Location | Course | Course Date | Duration | Early Bird Date |
|---|---|---|---|---|
| Melbourne | Programming Perl | Tue 2 Sep 2008 | 4 days | Mon 4 Aug 2008 |
| Sydney | Programming Perl | Tue 7 Oct 2008 | 4 days | Mon 8 Sep 2008 |
| Canberra | Programming Perl | Mon 24 Nov 2008 | 4 days | Mon 27 Oct 2008 |
For future dates, please see our training calendar.
This Perl tip and associated text is copyright Perl Training Australia. You may freely distribute this text so long as it is distributed in full with this Copyright noticed attached.
If you have any questions please don't hesitate to contact us:
| Email: | contact@perltraining.com.au |
| Phone: | 03 9354 6001 (Australia) |
| International: | +61 3 9354 6001 |
Copyright 2001-2008 Perl Training Australia. Contact us at contact@perltraining.com.au