Perl Packages and Modules
In Perl, each package has a separate symbol table. The defining syntax is:
package mypack;
This statement defines a package namedmypackpackage, and the names of all variables and subroutines defined after this point are stored in the symbol table associated with that package, until anotherpackagestatement is encountered.
Each symbol table has its own set of variable and subroutine names. The sets of names are independent of each other, so the same variable name can be used in different packages, representing different variables.
To access a variable in another package from one package, you can specify it in the form "package name + double colon (::) + variable name".
The default symbol table that stores the names of variables and subroutines is the one associated with the namedmainpackage. If other packages are defined in the program, and you want to switch back to the default symbol table, you can specify the main package again:package main;
In this way, the following program is as if no package had ever been defined, and variable and subroutine names are stored as usual.
In the following example, the file has the main and Foo packages. The special variable__PACKAGE__is used to output the package name:
Example
Executing the above program will produce the following result:
包名 : main 1 包名 : Foo 10 包名 : main 100 包名: main 10
BEGIN and END Blocks
The Perl language provides two keywords: BEGIN and END. They can each contain a set of scripts to be executed before or after the program body runs.
The syntax is as follows:
BEGIN { ... }
END { ... }
BEGIN { ... }
END { ... }
EachBEGINblock is executed after the Perl script is loaded and compiled, but before other statements are executed.
EachENDstatement block is executed before the interpreter exits.
BEGINandENDStatement blocks are especially useful when creating Perl modules.
If you still don't quite understand, we can look at an example:
Example
Executing the above program will produce the following result:
这是 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 file suffix for definitions is.pm。
Below we define a module Foo.pm, and the code is as follows:
Example
Regarding modules in Perl, the following points need attention:
functionrequireandusewill load a module.
@INCis a special built-in array in Perl that contains directory paths to the locations of library routines.
requireanduseThe function callsevalfunction to execute the code.
At the end1;must execute and return TRUE. This is necessary, otherwise an error is returned.
Require and Use Functions
Modules can be called through therequirefunction, as shown below:
Example
It can also be referenced through the use function:
Example
We notice that require references need to use the package name to specify the function, while use does not. The main differences between the two are:
- 1. require is used to load modules or Perl programs (the .pm suffix can be omitted, but the .pl suffix must be present)
- 2. The Perl use statement is introduced at compile time, while require is introduced at runtime.
- 3. When Perl use imports a module, it also imports the submodules of the module. However, require cannot import them; they need to be redeclared.
- 4. USE searches in the current default @INC. Once the 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 :: (double colon), the double colon will be used as a path separator, equivalent to / under Unix or \ under Windows. For example:
use MyDirectory::MyModule
By adding the following statement, you can use the module to export the symbols in the list:
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
You can easily create a Perl module with the h2xs tool that comes with the Perl distribution.
You can type h2xs in command-line mode to see its parameter list.
h2xs syntax format:
$ h2xs -AX -n ModuleName
Parameter description:
-AIgnore the autoload mechanism
-XIgnore XS elements
-nSpecify the name of the extension module
For example, if your module is inPerson.pmfile, use the following command:
$ h2xs -AX -n Person
Executing 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 file descriptions:
README: This file contains some installation information, module dependencies, copyright information, etc.
Changes: This file serves as the changelog file for your project.
Makefile.PL: This is the standard Perl Makefile constructor. It is used to create the Makefile.PL file to compile the module.
MANIFEST: This file is used to automatically build module distributions of the tar.gz type. This way you can publish your module on CPAN or distribute it to others. It contains a list of all files in this project.
Person.pm: This is the main module file, containing your mod_perl handler code.
Person.t: Some test scripts for the module. By default, it only checks whether the module is loaded, and you can add some new test units.
t/: Test files
lib/: Directory where the actual source code is stored
You can use the tar command (on Linux) to package the above directories into Person.tar.gz.
Installing Perl Modules
We can decompress and install thePerson.tar.gzfile, and the execution steps are as follows:
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. At this point, the entire compilation process is complete.
Other extensions