Each package in Perl has a separate symbol table, and the definition syntax is:
package mypack;
This statement defines a package named mypack . The names of all variables and subroutines defined thereafter are stored in the symbol table associated with the package until another package statement is encountered.
Each symbol table has its own set of variable and subroutine names. Each set of names is unrelated, so the same variable name can be used in different packages to represent different variables.
To access the variables of another package from one package, you can specify it by "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 and 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 will act as if the package had never been defined, and the names of variables and subroutines are stored as usual.
The files in the following example are main and Foo packages. The special variable __PACKAGE__ is used to output the package name:
Executing the above program, the output result is:
Package name: main 1 Package name: Foo 10 Package name: main 100 Package name: main 10
The Perl language provides two keywords: BEGIN and END. They can each contain a set of scripts for execution before or after the program body is run.
The syntax format is as follows:
BEGIN { ... }END { ... }BEGIN { ... }END { ... }
Each BEGIN module is executed after the Perl script is loaded and compiled but before other statements are executed.
Each END statement block is executed before the interpreter exits.
BEGIN and END statement blocks are particularly useful when creating Perl modules.
If you still don’t understand, we can look at an example:
Executing the above program, the output result is:
This is the BEGIN statement block. Begin and Block instances. This is the END statement block.
In Perl5, Perl packages are used to create modules.
A Perl module is a reusable package. The name of the module is the same as the name of the package, and the suffix of the defined file is .pm .
Below we define a module Foo.pm, the code is as follows:
There are a few things to note about modules in Perl:
The functions require and use will load a module.
@INC is a special array built into Perl that contains the directory path to the location of the library routines.
The require and use functions call the eval function to execute code.
1 at the end; execution returns TRUE, which is required, otherwise an error is returned.
Modules can be called through the require function, as shown below:
It can also be referenced through the use function:
We noticed that the require reference needs to use the package name to specify the function, but the use reference does not. The main difference between the two is:
1. require is used to load module or perl program (.pm suffix can be omitted, but .pl must be present)
2. The Perl use statement is introduced at compile time, and the require is introduced at runtime.
3. When Perl use introduces a module, it also introduces the submodule of the module. However, require cannot be introduced and must be redeclared
4. USE is searched in the current default @INC. Once the module is not in @INC, it cannot be introduced using USE, but require can specify the path.
5. When USE refers to a module, if the module name contains a :: double colon, the double colon will be used as a path separator, which is equivalent to / under Unix or under Windows. like:
use MyDirectory::MyModule
List symbols can be exported from the use module by adding the following statement:
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); #Default exported symbols sub bar { print "Hello $_[0]n" }sub blat { print "World $ _[0]n" }sub splat { print "Not $_[0]n" } # Not exported!1;
It is easy to create a Perl module through the tool h2xs 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:
-A ignores the autoload mechanism
-X ignore XS elements
-n specifies 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
Executing the above program will output:
Writing Person/lib/Person.pmWriting Person/Makefile.PLWriting Person/READMEWriting Person/t/Person.tWriting Person/ChangesWriting Person/MANIFEST
Under 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. Used to create a Makefile.PL file to compile the module.
MANIFEST: This file is used to automatically build tar.gz type module version distribution. In this way, you can take your module to CPAN and publish it or distribute it to others. It contains a list of all the files you have in this project.
Person.pm: This is the main module file that contains your mod_perl handler code.
Person.t: Some test scripts for this module. By default it just checks module loading, you can add some new test units.
t/: test file
lib/: directory where the actual source code is stored
You can use the tar (on Linux) command to package the above directory into Person.tar.gz.
We can decompress and install the Person.tar.gz file we just compressed. The steps are as follows:
tar xvfz Person.tar.gzcd Personperl Makefile.PLmakemake install
First run "perl Makefile.PL" to generate the 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 file into the system directory, and the entire compilation process ends.