Perl Packages and Modules

From WikiOD

Perl packages and modules[edit | edit source]

Each package in Perl has a separate symbol table, and the definition syntax is:

 package mypack;

This statement defines a package named my pack , and 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 variables and subroutine names. The names of each group are irrelevant. Therefore, the same variable name can be used in different packages and represent different variables.

To access the variables of another package from one package, it can be specified 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, when you want to switch back to use the default symbol table, you can re-specify the main package:

 package main;

In this way, the following program is as if a package has never been defined, and the names of variables and subroutines are stored as usual.

In the following example, the files have main and Foo packages. The special variable __PACKAGE__ is used to output the package name:

 #!/usr/bin/perl
 
 # main package
 $i = 1; 
 print "Package name: ", __PACKAGE__," $i\n"; 
 
 package Foo;
 # Foo package
 $i = 10; 
 print "Package name: ", __PACKAGE__," $i\n"; 
 
 package main;
 # Re-specify the main package
 $i = 100; 
 print "Package name: ", __PACKAGE__," $i\n"; 
 print "Package name: ", __PACKAGE__," $Foo::i\n"; 
 
 1;

Execute the above program, the output result is:

 Package name: main 1
 Package name: Foo 10
 Package name: main 100
 Package name: main 10

BEGIN and END modules[edit | edit source]

Perl language provides two keywords: BEGIN, END. They can each contain a set of scripts for execution before or after the program body runs.

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.
  • The BEGIN and END statement 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 and Block instance\n";
 
 BEGIN { 
     print "This is a BEGIN statement block\n" 
 }
 
 END { 
     print "This is an END statement block\n" 
 }
 
 1;

Execute the above program, the output result is:

 This is the BEGIN statement block
 Begin and Block examples
 This is the END statement block

What is a Perl module?[edit | edit source]

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 package name, and the defined file suffix 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;

The following points should be noted 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 routine.
  • The require and use functions call the eval function to execute code.
  • At the end 1; the execution returns TRUE, which is necessary, otherwise an error is returned.

Require and Use functions[edit | edit source]

The module can be called through the require function, as shown below:

 #!/usr/bin/perl
 
 require Foo;
 
 Foo::bar( "a" );
 Foo::blat( "b" );

It can also be referenced through the use function:


 #!/usr/bin/perl
 
 use Foo;
 
 bar( "a" );
 blat( "b" );
We noticed that require references need to use package names to specify functions, 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 have) * 2, Perl use statement is introduced at compile time, require is introduced at runtime * 3. When Perl use introduces the module, it also introduces the sub-modules of the module. While require cannot be introduced, it must be re-declared * 4. USE is searched in the current default @INC. Once the module is not in @INC, it cannot be imported with USE, but require can specify the path * 5. When USE refers to a module, if the module name contains :: 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
You can export list symbols from the module by adding the following statement use module:
 require Exporter;
 @ISA = qw(Exporter);
The @EXPORT array contains the names of variables and functions that are 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;

Create Perl module[edit | edit source]

You can easily create a Perl module through the h2xs tool that comes with the Perl distribution. You can type h2xs in the command line mode to see its parameter list. h2xs syntax format:
 $ h2xs -AX -n ModuleName
Parameter Description: * -A ignore the autoload mechanism * -X ignores 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.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 directories and file descriptions: * 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 create Makefile.PL file to compile the module. * MANIFEST: This file is used to automatically build the tar.gz type of module version distribution. So you can get your module to CPAN to publish or distribute to others. It contains a list of all the files you have in this project. * Person.pm: This is the main module file, which contains your mod_perl handler code. * Person.t: Some test scripts for this module. By default it just checks the loading of the module, 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 Person.tar.gz.

Install Perl modules[edit | edit source]

We can decompress and install the Person.tar.gz file 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 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 to the system directory, and the whole compilation process is over.