Perl packages and modules
Each package in Perl has a separate symbol table, and the definition syntax is:
package mypack;
This statement defines a package named mypack, and all the symbols defined thereafter The names of variables and subroutines 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 irrelevant, 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 look like starting from As if no package has been defined, the names of variables and subroutines are stored as usual.
The files in the following example include main and Foo packages. The special variable __PACKAGE__ is used to output the package name:
#!/usr/bin/perl # main 包 $i = 1; print "包名 : " , __PACKAGE__ , " $i\n"; package Foo; # Foo 包 $i = 10; print "包名 : " , __PACKAGE__ , " $i\n"; package main; # 重新指定 main 包 $i = 100; print "包名 : " , __PACKAGE__ , " $i\n"; print "包名: " , __PACKAGE__ , " $Foo::i\n"; 1;
Execute the above program, the output result is:
包名 : main 1 包名 : Foo 10 包名 : main 100 包名: main 10
BEGIN and END module
Perl language provides two keywords: BEGIN, 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 loaded and compiled by the Perl script But it is executed before other statements are executed.
Each END statement block is executed before the interpreter exits. The
BEGIN and END blocks are particularly useful when creating Perl modules.
If you still don’t understand, we can look at an example:
#!/usr/bin/perl package Foo; print "Begin 和 Block 实例\n"; BEGIN { print "这是 BEGIN 语句块\n" } END { print "这是 END 语句块\n" } 1;
Execute the above program, the output result is:
这是 BEGIN 语句块 Begin 和 Block 实例 这是 END 语句块
What are Perl modules?
Perl5 uses Perl packages to create modules.
Perl module is a reusable package. The name of the module is the same as the name of the package. The suffix of the defined file is .pm.
Below we define a module Foo.pm, the code is as follows:
#!/usr/bin/perl package Foo; sub bar { print "Hello $_[0]\n" } sub blat { print "World $_[0]\n" } 1;
You need to pay attention to the following points 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 the code.
End 1; Execution returns TRUE, which is required, otherwise an error is returned.
Require and Use functions
The module can be called through the require function, as follows:
#!/usr/bin/perl require Foo; Foo::bar( "a" ); Foo::blat( "b" );
It can also be referenced through the use function:
<pre> #!/usr/bin/perl use Foo; bar( "a" ); blat( "b" );
We noticed that require reference needs to use the package name to specify the function, but use 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. Perl use statement is introduced during compilation, and require is introduced during runtime
3. When Perl use introduces modules, it also introduces submodules of the modules. And require cannot be imported, it must be re-declared
4. USE is found in the current default @INC. Once the module is not in @INC, it cannot be imported using USE Yes, 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 the path separator, which is equivalent to the path separator under Unix. /or \ under Windows. For example:
use MyDirectory::MyModule
You can export list symbols from the module by adding the following statement use module:
require Exporter; @ISA = qw(Exporter);
@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;
Create Perl module
It can be easily created through the tool h2xs that comes with Perl distribution. A Perl module.
You can type h2xs in command line mode to see its parameter list.
h2xs Syntax format:
$ 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
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
You can see it in the Person directory Go to the newly added directory and file description:
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. 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 The above directory can be packaged as Person.tar.gz using the tar (on Linux) command.
Install the Perl module
We can decompress and install the Person.tar.gz file we just compressed. The steps are as follows:
tar xvfz Person.tar.gz cd Person perl Makefile.PL make make 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 files to the system directory. That's it. The entire compilation process ends.