Libraries, Modules, Packages, and Pragmas


Why re-invent the wheel? There are numerous libraries and modules available for Perl that will save you much time and effort in accomplishing many common tasks. In writing your own programs, you may also wish to divide them into modules to reduce complexity. Most programers find that the divide-and-conquer strategy of seperating a program's tasks into smaller sub-components makes the job much easier and understandable.

One category of modules called pragmas directs the Perl compiler to operate in different ways. You can think of them as similar to Perl's command-line switches but more advanced.



Libraries

Libraries are files (scripts in Perl) containing groups of related functions. This old mechanism for loading and running perl scripts inside another script is discouraged in favor of modules. The format is 'required' followed by the name of the script.

Example:
required my_other_script.pl;




Modules

There are numerous modules available for Perl that will save you much effort. The most popular ones are included in the latest distributions of Perl5. Others can be found under the modules directory at CPAN - the Comprehensive Perl Archive Network - on the Internet.

The syntax for using a module is:
use Module;
use Module LIST;
use Module qw(first second third);

where LIST represents functions or sets of functions to be imported from the Module.

Note: The qw() function (quoted words) takes a list of arguments, wraps each in quotes, and puts them in a list. It's used as a shortcut. The following are equivalent:

@List = ('first', 'second', 'third');
@List = qw(first second third);

The special variable @INC contains a list of directories for where to look for perl modules and libraries. When perl is installed, the locations of the installed modules are stored in @INC. You can modify @INC in your scripts to specify additional places to look for modules. By default, the current path is also included in @INC.



Importing

The global namespace contains the names of all variables used in your perl script. Variables used in modules are typically assigned to namespaces other than global. This is to prevent an accidental overlap of the variable names used in your script and those of the module. This is done internally in a module by using the package keyword.
package directoryname::modulename;

# any functions or variables following this statement inside the module 
# must be specified by directoryname::modulename::variablename

When variables or functions in a module are imported into your script, it eliminates the need for using the long prefixes to specify them. (Their names are imported into the current global namespace of your script.) You typically import the variables and functions from a module that you wish to use in your current script. Some of the variables and functions of a module will be imported automatically, depending on the module, whenever the module is used.

Example:

use CGI qw(:standard);

# Use the CGI module and import all functions and variables associated with the :standard label.




Pragmas

Pragmas are used in Perl and other languages as directives telling the compiler to change how it operates. In perl, pragmas are modules named in all lowercase letters. The most commonly used pragma is strict. It requires that all variables be defined before they are used. This can save a lot of frustration with mispelled variables. It also adds checks on syntax when using subroutines and references.

Example:
use strict;      # turn on strict pragma
my $a = 3;       # define and intialize variable $a
my ($b, $c, $d, @my_list); # define these variables

$new_list = (3,4,5);  # throws an error because
                      # $new_list has not been defined
if($a){               # the variable $a can be used