Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Perl Packages and Modules

Perl Packages and Modules

In Perl, each package has its own symbol table. The definition syntax is:



package mypack;

This statement defines a package named mypack of which all the names of variables and subroutines defined afterwards are stored in the symbol table associated with this package, until another package is encountered.

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

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

The default symbol table that stores the names of variables and subroutines is associated with the package named

main

If other packages are defined in the program, when you want to switch back to using the default symbol table, you can re-specify the main package:



package main;

In this way, the following program is as if no package had ever been defined, and the names of variables and subroutines are stored as usual.

In the following example, the file has the packages main and Foo. 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 the main package

$i

=

100

;

print

"

Package name :

"

,

PACKAGE

,

"

$i

\n

"

;

print

"

Package name:

"

,

PACKAGE

,

"

$Foo

::i

\n

"

;

1

;

Running the above program produces the following output:



包名 : main 1

包名 : Foo 10

包名 : main 100

包名: main 10


BEGIN and END blocks

The Perl language provides two keywords: BEGIN and END. Each can contain a block of script that is executed before or after the main program body runs.

The syntax format is as follows:



BEGIN { ... }

END { ... }

BEGIN { ... }

END { ... }

If you don't quite understand yet, let's look at an example:

Example

#!/usr/bin/perl

package

Foo

;

print

"

BEGIN and END block example

\n

"

;

BEGIN

{

print

"

This is the BEGIN block

\n

"

}

END

{

print

"

This is the END block

\n

"

}

1

;

Running the above program produces the following output:



这是 BEGIN 语句块

Begin 和 Block 实例

这是 END 语句块


What is a Perl module?

In Perl 5, Perl packages are used to create modules.

A Perl module is a reusable package; the module name is the same as the package name, and the defining file has the extension .pm。

Below we define a module Foo.pm, with the following code:

Example

#!/usr/bin/perl

package

Foo

;

sub

bar

{

print

"

Hello

$_

[0]

\n

"

}

sub

blat

{

print

"

World

$_

[0]

\n

"

}

1

;

The following points should be noted about modules in Perl:


require and use Functions

A module can be called through the require function, as shown below:

Example

#!/usr/bin/perl

require

Foo

;

Foo

::

bar

(

"

a

"

)

;

Foo

::

blat

(

"

b

"

)

;

It can also be referenced through the use function:

Example

#!/usr/bin/perl

use Foo

;

bar

(

"

a

"

)

;

blat

(

"

b

"

)

;

We note that referencing with require requires using the package name to specify the function, while use does not. The main differences between the two are:

By adding the following statement, the use module can export a list of symbols from the module:



require Exporter;

@ISA = qw(Exporter);

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



package Module;



require Exporter;

@ISA = qw(Exporter);

@EXPORT = qw(bar blat);  # 默认导出的符号



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

A Perl module can be created easily using the h2xs tool that comes with the Perl distribution.

You can type h2xs at the command line to see its list of parameters.

h2xs syntax:



$ h2xs -AX -n  ModuleName

Parameter Description:

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



$ h2xs -AX -n Person

Running the above program 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

In the Person directory you can see the newly added directories and files, described as follows:

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


Installing Perl Modules

We can extract and install the Person.tar.gz file we just compressed, 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 required library files;

Then use "make test" to test whether the compilation result is correct; finally run "make install" to install the library files into the system directory, which completes the whole compilation process.