Easy Tutorial
❮ Home Perl Arrays ❯

Perl Packages and Modules

In Perl, each package has a separate symbol table, defined with the syntax:

package mypack;

This statement defines a package named mypack. All variables and subroutine names defined thereafter are stored in the symbol table associated with this package until another package statement is encountered.

Each symbol table has its own set of variables and subroutine names, which are unrelated. Therefore, the same variable names can be used in different packages to represent different variables.

To access a variable from another package, you can specify it using the format "package name + double colon (::) + variable name".

package main;

This way, the subsequent program behaves as if no package has been defined, with variables and subroutine names stored as usual.

In the following example, the file contains the main and Foo packages. The special variable _PACKAGE_ is used to output the package name:

Example

#!/usr/bin/perl

# main package
$i = 1;
print "Package Name: ", __PACKAGE__, " $i\n";

package Foo;
# Foo package
$i = 10;
print "Package Name: ", __PACKAGE__, " $i\n";

package main;
# Re-specify main package
$i = 100;
print "Package Name: ", __PACKAGE__, " $i\n";
print "Package Name: ", __PACKAGE__, " $Foo::i\n";

1;

Executing the above program outputs:

Package Name: main 1
Package Name: Foo 10
Package Name: main 100
Package Name: main 10

BEGIN and END Blocks

Perl provides two keywords: BEGIN and END. They can contain scripts to be executed before the program runs or after it finishes.

The syntax is as follows:

BEGIN { ... }
END { ... }
BEGIN { ... }
END { ... }

If you are not yet clear, let's look at an example:

Example

#!/usr/bin/perl

package Foo;
print "BEGIN and END Block Example\n";

BEGIN {
    print "This is a BEGIN block\n"
}

END {
    print "This is an END block\n"
}

1;

Executing the above program outputs:

This is a BEGIN block
BEGIN and END Block Example
This is an END block

What is a Perl Module?

In Perl 5, modules are created using Perl packages.

A Perl module is a reusable package with the same name as the package, defined in a file with the .pm extension.

Below, we define a module Foo.pm, as shown:

Example

#!/usr/bin/perl

package Foo;
sub bar {
   print "Hello $_[0]\n"
}

sub blat {
   print "World $_[0]\n"
}
1;

A few points to note about modules in Perl:


Require and Use Functions

Modules can be called using the require function, as shown:

Example

#!/usr/bin/perl

require Foo;

Foo::bar("a");
Foo::blat("b");

They can also be referenced using the use function:

Example

#!/usr/bin/perl

use Foo;

bar("a");
blat("b");

Note that require requires specifying the package name for the function, while use does not. The main differences between them are:

  1. require is used to load a module or Perl script (the .pm extension can be omitted, but .pl must be present).
  2. Perl's use statement is imported at compile time, while require is at runtime.
  3. use imports the module and its sub-modules, whereas require does not and needs to be re-declared.
  4. use searches in the current default @INC, but require can specify a path if the module is not in @INC. When using the use keyword to reference a module, if the module name contains :: (double colons), these double colons serve as path separators, equivalent to / in Unix or \ in Windows. For example:
    use MyDirectory::MyModule;
    

By adding the following statements, you can export symbols from the module:

require Exporter;
@ISA = qw(Exporter);

The @EXPORT array contains the names of variables and functions that are exported by default:

package Module;

require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(bar blat);  # Default exported symbols

sub bar { print "Hello $_[0]\n" }
sub blat { print "World $_[0]\n" }
sub splat { print "Not $_[0]\n" }  # Not exported!

1;

Creating a Perl Module

Creating a Perl module is straightforward using the h2xs tool, which comes with Perl distributions.

You can view the list of parameters for h2xs by typing it in the command line.

Syntax for h2xs:

$ h2xs -AX -n ModuleName

Parameter explanations:

For example, if your module is in the file Person.pm, use the following command:

$ h2xs -AX -n Person

Executing the above command will output:

Writing Person/lib/Person.pm
Writing Person/Makefile.PL
Writing Person/README
Writing Person/t/Person.t
Writing Person/Changes
Writing Person/MANIFEST

Under the Person directory, you will see the newly added directories and files:

You can use the tar command (on Linux) to package the above directories into Person.tar.gz.


Installing a Perl Module

You can unzip and install the previously compressed Person.tar.gz file with the following steps:

tar xvfz Person.tar.gz
cd Person
perl Makefile.PL
make
make install

First, run "perl Makefile.PL" to generate a Makefile in the current directory; then run "make" to compile and create the necessary library files; next, use "make test" to test if the compilation results are correct; finally, run "make install" to install the library files into the system directory. This completes the entire compilation process.

❮ Home Perl Arrays ❯