Perl packages and modules
Each package in Perl has a separate symbol table that defines the syntax as:
package mypack;
This statement defines a package named mypack, and the names of all variables and sub-programs 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 variables, sub-program names, and the group names are irrelevant, so you can use the same variable names in different packages, representing different variables.
The variable that accesses another package from one package can be specified by the "package name and double colon (:: ) and variable name".
The default symbol table for storing the names of variables and sub-programs is associated with a package named main. If you define another package in the program, you can re-specify the main package when you want to switch back to using the default symbol table:
package main;
In this way, the next program is as if the package has never been defined, and the names of variables and sub-programs are stored as usual.
The following Chinese have main and Foo packages. Special variable __PACKAGE__ used to output package names:
#!/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;
The above procedure is performed and the output is:
包名 : main 1 包名 : Foo 10 包名 : main 100 包名: main 10
BEGIN and END modules
The Perl language provides two keywords: BEGIN, END. They can contain a separate set of scripts for execution before or after the program runs.
The syntax format is as follows:
BEGIN { ... } END { ... } BEGIN { ... } END { ... }
-
Each BEGIN module is loaded and compiled by the Perl script but executed before other statements are executed.
-
Each END statement block executes before the interpreter exits.
-
BEGIN and END statement blocks are especially useful when creating Perl modules.
If you 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;
The above procedure is performed and the output is:
这是 BEGIN 语句块 Begin 和 Block 实例 这是 END 语句块
What is a Perl module?
Perl5 uses perl packages to create modules.
The Perl module is a reusable package with the same name as the package name and a file suffix of .pm.
Here's what we've defined Foo.pm module, as follows:
#!/usr/bin/perl package Foo; sub bar { print "Hello $_[0]\n" } sub blat { print "World $_[0]\n" } 1;
There are a few points in Perl that you need to be aware of about modules:
-
The functions need and use load a module.
-
@INC is a special array built into Perl that contains a directory path to the location of the library routine.
-
The require and use functions call the eval function to execute the code.
-
End 1; Execution returns TRUE, which is necessary, otherwise an error is returned.
Require and Use functions
Modules can be called by the require function, as follows:
#!/usr/bin/perl require Foo; Foo::bar( "a" ); Foo::blat( "b" );
You can also refer to it through the use function:
<pre> #!/usr/bin/perl use Foo; bar( "a" ); blat( "b" );
We note that the require reference needs to specify a function using a package name, wherease does not, and the main difference between the two is:
- 1, need is used to load module or perl program (.pm suffix can be omitted, but .pl must have)
- 2, Perl use statement is introduced at compile time, require is introduced at runtime
- 3, Perl use introduces the module, but also introduces the module's sub-module. Require, on the other hand, cannot be introduced to be re-declared
- 4, USE is in the current default @INC to find, once the module is not in the @INC, the use of USE can not be introduced, but require can specify the path
-
5, USE reference module, if the module name contains: : double colon, the double colon will be used as a path separator, equivalent to Unix under / or Windows under the .
Such as:
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 the variables and functions that are 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 a Perl module
The tool h2xs that come with Perl distribution can simply create a Perl module.
You can type h2xs in command-line mode to see a list of its parameters.
h2xs syntax format:
$ h2xs -AX -n ModuleName
Description of the parameters:
-
-A Ignore the autoload mechanism
-
-X ignores the XS element
-
-n Specify the name of the extension
For example, if your module is in Person.pm file, use the following command:
$ h2xs -AX -n Person
Executing the above procedure 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
Under the Person directory you can see the newly added directory and file description:
-
README: This file contains some installation information, module dependencies, copyright information, etc.
-
Changes: This file serves as a changelog file for your project.
-
Makefile.PL: This is the standard Perl Makefile constructor. Used to Makefile.PL file to compile the module.
-
MANIFEST: This file is used to automatically build .gz version distribution of the tar type. T his way you can take your modules to CPAN for release or distribution to others. It contains a list of all the files you have in this project.
-
Person.pm: This is the main module file mod_perl your handle handle code.
-
Person.t: Some test scripts for this module. By default it just checks the module load, and you can add some new test units.
-
t/ : Test file
-
lib/ : The directory where the actual source code is stored
You can use the tar (on Linux) command to package the above directory as a .tar.gz.
Install the Perl module
We can unzip and install the Person .tar.gz file that we just compressed, as follows:
tar xvfz Person.tar.gz cd Person perl Makefile.PL make make install
Start by running "perl Makefile.PL" to generate Makefile in the current directory;
Then run "make" to compile and create the required library files;
Then test the compilation results correctly with "make test" and finally run "make install" to install the library file into the system directory until the entire compilation process is complete.