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
;
"
Package name :
"
,
PACKAGE
,
"
$i
\n
"
;
package
Foo
;
Foo package
$i
=
10
;
"
Package name :
"
,
PACKAGE
,
"
$i
\n
"
;
package
main
;
Re-specify the main package
$i
=
100
;
"
Package name :
"
,
PACKAGE
,
"
$i
\n
"
;
"
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 { ... }
-
Each BEGIN The BEGIN block is executed after the Perl script is loaded and compiled but before other statements are executed.
-
Each END The END block is executed just before the interpreter exits.
-
BEGIN and END These blocks are particularly useful when creating Perl modules.
If you don't quite understand yet, let's look at an example:
Example
#!/usr/bin/perl
package
Foo
;
"
BEGIN and END block example
\n
"
;
BEGIN
{
"
This is the BEGIN block
\n
"
}
END
{
"
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
{
"
Hello
$_
[0]
\n
"
}
sub
blat
{
"
World
$_
[0]
\n
"
}
1
;
The following points should be noted about modules in Perl:
-
function require and use will load a module.
-
@INC is a special built-in array in Perl that contains the directory paths pointing to the locations of library routines.
-
require and use Function call eval function to execute the code.
-
end 1; The execution must return TRUE; otherwise an error is returned.
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:
- 1. require is used to load a module or a Perl program (the .pm extension can be omitted, but .pl must be included)
- 2. The Perl use statement is imported at compile time, while require is imported at runtime
- 3. When Perl use imports a module, it also imports the module's sub-modules. require cannot do this; it must be declared again
- 4. use searches in the current default @INC; once a module is not in @INC, it cannot be imported with use, but require can specify a path
- 5. When use references a module, if the module name contains the double colon ::, the double colon is used as a path separator, equivalent to / on Unix or \ on Windows. For example:
use MyDirectory::MyModule
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:
-
-A Ignore the autoload mechanism
-
-X Ignore XS elements
-
-n Specify the name of the extension module
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:
-
README: This file contains some installation information, module dependencies, copyright information, etc.
-
Changes: This file serves as the change log (changelog) for your project.
-
Makefile.PL: This is the standard Perl Makefile builder, used to create the Makefile.PL file to compile the module.
-
MANIFEST: This file is used to automatically build a tar.gz type module distribution. This way you can publish your module to CPAN or distribute it to others. It contains a list of all the files in your project.
-
Person.pm: This is the main module file, containing your mod_perl handler code (handler code).
-
Person.t: Some test scripts for the module. By default it only checks that the module loads; you can add some new test units.
-
t/: Test files
-
lib/: The directory where the actual source code is stored
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.