Perl modules

[ 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.

Module Uses

Perl modules can do just about anything. In general, however, there are three main uses for modules:

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.

Where does Perl look for modules?

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:

Since use statements occur before regular Perl code is executed, modifying @INC directly usually does not have the desired effect.

Finding installed modules

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 ]


Upcoming courses

Location Course Course Date Duration Early Bird Date
Sydney Programming Perl Mon 22 Mar 2010 5 days
Brisbane Programming Perl Mon 12 Apr 2010 5 days
Canberra Programming Perl Mon 19 Apr 2010 5 days
Melbourne Programming Perl Mon 16 Aug 2010 5 days Mon 12 Jul 2010
Sydney Programming Perl Mon 6 Sep 2010 5 days Tue 3 Aug 2010
Canberra Programming Perl Mon 20 Sep 2010 5 days Tue 24 Aug 2010

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

Valid XHTML 1.0 Valid CSS