Perl - Modules
In a Nutshell - CIW Course Section 2, Part B3, Chapter 2
Modules Overview
A module is a logical extension in the use of packages. Essentially it involves putting a package in the form of a subroutine or subroutines in an external file which may then be included in another script. Filenames for Perl modules will have the ".pm" extension.
There are two methods for including a module in your script, the "use" statement and the "require" statement. Each has it's advantages and disadvantages.
Require Statement
The Require Statement is, perhaps, the simpler of the two methods for including a module in your script but stipulates that any routines or variables within the module must be referenced by way of the symbol table.
#!c:\perl\bin\perl.exe
require getTime;
print getTime::currentTime();
The above is going to call a subroutine named currentTime from the module getTime. As we have used the require statement it is necessary to employ the getTime::currentTime() syntax to invoke the routine. This is because the symbol table from getTime has not been imported.
I discovered, whilst debugging, that getTime->currentTime will work if you haven't included the parentheses for the function call.
package getTime;
1;
sub currentTime
{
@mytime = localtime(time);
$secs = $mytime[0];
$mins = $mytime[1];
$hours = $mytime[2];
return formatTime($hours, $mins, $secs);
}
This is the code in the module stored in file getTime.pm. The important item to note is the digit 1 on the line after the package declaration. This is required in all modules. The module also contains the code for the function formatTime but I have omitted this for clarity.
Use Statement
The main difference with the use statement is that it will import the symbol table from the module it is referencing. What this means in practical terms is that subroutines and variables in the module will become available to the calling script, as the list of names is imported to the symbol table of the calling script.
#!c:\perl\bin\perl.exe
use Gettime;
print currenttime();
The only difference you will see in the calling script, is that we can now reference the function "currentTime()" directly. We don't need to include the "Gettime" name to identify it.
package Gettime;
1;
require Exporter;
@ISA = ("Exporter");
@EXPORT = ("currenttime");
@EXPORT_OK = ("\$token");
sub currenttime
{
@mytime = localtime(time);
$secs = $mytime[0];
$mins = $mytime[1];
$hours = $mytime[2];
$time = "";
return formatTime($hours, $mins, $secs);
}
To enable this functionality, we need to add a few extra lines to the module being called. We have made mention of the calling script importing the symbol table from the module. In fact, it is the module that exports it's symbol table. To do this we need to add a reference to the "Exporter" module and set a few variables that this module needs to operate correctly.
Don't make the mistake I did, and put the extra lines before the package definition, it doesn't work.
localtime function
This has nothing to do with modules, but I have used it in the examples. While putting this page together I wanted a simple function that could display the current time. Nothing is ever simple, what I discovered was the localtime function.

