Perl 简明教程

Perl - Packages and Modules

What are Packages?

package 语句将当前命名上下文切换到指定的命名空间(符号表)。因此 −

The package statement switches the current naming context to a specified namespace (symbol table). Thus −

  1. A package is a collection of code which lives in its own namespace.

  2. A namespace is a named collection of unique variable names (also called a symbol table).

  3. Namespaces prevent variable name collisions between packages.

  4. Packages enable the construction of modules which, when used, won’t clobber variables and functions outside of the modules’s own namespace.

  5. The package stays in effect until either another package statement is invoked, or until the end of the current block or file.

  6. You can explicitly refer to variables within a package using the :: package qualifier.

以下是文件中包含主包和 Foo 包的示例。此处已经使用特殊变量 PACKAGE 来打印包名。

Following is an example having main and Foo packages in a file. Here special variable PACKAGE has been used to print the package name.

#!/usr/bin/perl

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

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

package main;
# This is again main package
$i = 100;
print "Package name : " , __PACKAGE__ , " $i\n";
print "Package name : " , __PACKAGE__ ,  " $Foo::i\n";

1;

当以上代码执行后,它将产生以下结果 −

When above code is executed, it produces the following result −

Package name : main 1
Package name : Foo 10
Package name : main 100
Package name : main 10

BEGIN and END Blocks

您可以定义任意数量的 BEGIN 和 END 命名代码块,它们分别充当构造函数和析构函数。

You may define any number of code blocks named BEGIN and END, which act as constructors and destructors respectively.

BEGIN { ... }
END { ... }
BEGIN { ... }
END { ... }
  1. Every BEGIN block is executed after the perl script is loaded and compiled but before any other statement is executed.

  2. Every END block is executed just before the perl interpreter exits.

  3. The BEGIN and END blocks are particularly useful when creating Perl modules.

下面的示例演示了它的用法:

Following example shows its usage −

#!/usr/bin/perl

package Foo;
print "Begin and Block Demo\n";

BEGIN {
   print "This is BEGIN Block\n"
}

END {
   print "This is END Block\n"
}

1;

当以上代码执行后,它将产生以下结果 −

When above code is executed, it produces the following result −

This is BEGIN Block
Begin and Block Demo
This is END Block

What are Perl Modules?

Perl 模块是定义在一个库文件中的可重用包,该文件的名称与包名称相同,但扩展名为 .pm。

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

名为 Foo.pm 的 Perl 模块文件可能包含如下语句。

A Perl module file called Foo.pm might contain statements like this.

#!/usr/bin/perl

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

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

有关 Perl 模块的一些重要要点

Few important points about Perl modules

  1. The functions require and use will load a module.

  2. Both use the list of search paths in @INC to find the module.

  3. Both functions require and use call the eval function to process the code.

  4. The 1; at the bottom causes eval to evaluate to TRUE (and thus not fail).

The Require Function

可以通过调用 require 函数如下加载模块:

A module can be loaded by calling the require function as follows −

#!/usr/bin/perl

require Foo;

Foo::bar( "a" );
Foo::blat( "b" );
You must have noticed that the subroutine names must be fully qualified to call them. It would be nice to enable the subroutine bar and blat to be imported into our own namespace so we wouldn’t have to use the Foo

qualifier.

The Use Function

可以通过调用 use 函数加载模块。

A module can be loaded by calling the use function.

#!/usr/bin/perl

use Foo;

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

注意,我们不必完全限定包的函数名称。 use 函数将从模块中导出一个符号列表,该列表给出了模块中添加的几个语句。

Notice that we didn’t have to fully qualify the package’s function names. The use function will export a list of symbols from a module given a few added statements inside a module.

require Exporter;
@ISA = qw(Exporter);

然后,通过填充名为 @EXPORT 的列表变量,提供一个符号列表(标量、列表、哈希、子例程等):例如:

Then, provide a list of symbols (scalars, lists, hashes, subroutines, etc) by filling the list variable named @EXPORT: For Example −

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;

Create the Perl Module Tree

当您准备发布您的 Perl 模块时,便有创建 Perl 模块树的标准方式。这是通过使用 h2xs 实用程序完成的。该实用程序随 Perl 一起提供。以下是如何使用 h2xs 的语法:

When you are ready to ship your Perl module, then there is standard way of creating a Perl Module Tree. This is done using h2xs utility. This utility comes along with Perl. Here is the syntax to use h2xs −

$h2xs -AX -n  ModuleName

例如,如果您的模块在 Person.pm 文件中,则只需发出以下命令:

For example, if your module is available in Person.pm file, then simply issue the following command −

$h2xs -AX -n Person

这会产生以下结果 −

This will produce the following result −

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

以下是这些选项的说明:

Here is the descritpion of these options −

  1. -A omits the Autoloader code (best used by modules that define a large number of infrequently used subroutines).

  2. -X omits XS elements (eXternal Subroutine, where eXternal means external to Perl, i.e., C).

  3. -n specifies the name of the module.

因此,上述命令在 Person 目录中创建以下结构。实际结果如上所示。

So above command creates the following structure inside Person directory. Actual result is shown above.

  1. Changes

  2. Makefile.PL

  3. MANIFEST (contains the list of all files in the package)

  4. README

  5. t/ (test files)

  6. lib/ ( Actual source code goes here

最后,您将 tar 此目录结构放入 Person.tar.gz 文件中,之后便可进行交付。您将需要使用适当的说明更新 README 文件。您还可以在 t 目录中提供一些测试示例文件。

So finally, you tar this directory structure into a file Person.tar.gz and you can ship it. You will have to update README file with the proper instructions. You can also provide some test examples files in t directory.

Installing Perl Module

以 tar.gz 文件的形式下载 Perl 模块。使用以下序列安装已以 Person.tar.gz 文件形式下载的 Person.pm 任何 Perl 模块。

Download a Perl module in the form tar.gz file. Use the following sequence to install any Perl Module Person.pm which has been downloaded in as Person.tar.gz file.

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

Perl 解释器具有一个目录列表,用于搜索模块(全局数组 @INC)。

The Perl interpreter has a list of directories in which it searches for modules (global array @INC).