Latest web development tutorials

Perl packages and modules

Perl Each package has a separate symbol table definition syntax is:

package mypack;

This statement defines a package calledmypack in the name of all the variables and subroutines defined hereinafter are stored in the symbol table associated with the package, the packageuntil it encounters another statement so far.

Each symbol table has its own set of variables, subroutines, each group name is not relevant, so you can use the same variable name in different packages, which represent different variables.

Access from one package to another package variable, you can specify the "package name + double colon (::) + variable name" approach.

Storing variables and subroutine names default symbol table associated with the package is calledmain-linked.If the program is defined in other packages, when you want to switch back to using the default symbol table, you can reassign the main package:

package main;

Thus, the next program as if never defined packages, variables and subroutine names as usual storage.

The following example file has main and Foo package.__PACKAGE__ Special variable for output package names:


#  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"; 


The above program, the output is:

包名 : main 1
包名 : Foo 10
包名 : main 100
包名: main 10

BEGIN and END modules

Perl language provides two keywords: BEGIN, END. They can be a separate set of scripts that execute before the program runs or body after running.

Syntax is as follows:

BEGIN { ... }
END { ... }
BEGIN { ... }
END { ... }
  • EachBEGIN block but executed before other statements executed after the Perl script loaded and compiled.

  • EachEND block of statements executed before the interpreter quits.

  • BEGIN and ENDblock is particularly useful when you create a Perl module.

If you do not understand big, we can see instances:


package Foo;
print "Begin 和 Block 实例\n";

    print "这是 BEGIN 语句块\n" 

END { 
    print "这是 END 语句块\n" 


The above program, the output is:

这是 BEGIN 语句块
Begin 和 Block 实例
这是 END 语句块

What is a Perl module?

Perl5 with Perl package to create a module.

Perl module is a reusable package, the module name and the same package name, file extension defined

Below we define a module, code as follows:


package Foo;
sub bar { 
   print "Hello $_[0]\n" 

sub blat { 
   print "World $_[0]\n" 

Perl modules concerning to note the following:

  • Function anduse will requirea load module.

  • @INC Perl is a special built-in array that contains the directory path to the location of the library routines lies.

  • require and usefunction callsevalfunction to execute code.

  • End1; execution returns TRUE, this is a must, otherwise it returns an error.

Require Use and function

Module can be invoked by therequire function as follows:


require Foo;

Foo::bar( "a" );
Foo::blat( "b" );

You can also use the function to refer to:


use Foo;

bar( "a" );
blat( "b" );

We note the need to require a reference to the package name specified function, and do not need to use, the main difference between the two is:

  • 1, require for loading perl module or program (.pm suffix can be omitted, but there must .pl)
  • 2, Perl use statement is compiled when introduced, require runtime is introduced
  • 3, Perl use the introduction of the module, but also the introduction of sub-module module. And it can not require the introduction to the re-statement
  • 4, USE is to look at the current default @INC inside, once the module is not in the words @INC with USE is not introduced, but may require a specified path
  • 5, USE referenced module, if the module name contains :: double colon, the colon will double as a path separator, the equivalent of the Unix / or under Windows \. Such as:

    use MyDirectory::MyModule

You can export a list of symbols from the module by adding the following statement to use modules:

require Exporter;
@ISA = qw(Exporter);

@EXPORT Array contains default variables and functions derived from the name:

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!


Create a Perl module

You can easily create a Perl module Perl distribution comes with a tool h2xs.

You can type in the command line mode h2xs look at its parameter list.

h2xs syntax:

$ h2xs -AX -n  ModuleName

Parameter Description:

  • -A Ignored autoload mechanism

  • -X Ignored XS element

  • -n Specifies the extension name

For example, if your file, use the following command:

$ h2xs -AX -n Person

The above program will output:

Writing Person/lib/
Writing Person/Makefile.PL
Writing Person/README
Writing Person/t/Person.t
Writing Person/Changes
Writing Person/MANIFEST

Person under the directory you can see the newly added directories and files Explanation:

  • README: This file contains information about installation, module dependencies, copyright information, and so on.

  • Changes: This change log file as your project (changelog) file.

  • Makefile.PL: This is the standard Perl Makefile builder. Used to create a Makefile.PL file to compile the module.

  • MANIFEST: This file is used to automatically build tar.gz type of module version distribution. So you can put your CPAN module to get published or distributed to others. It contains a list of all of your files in this project.

  • This is the main module file that contains the code for your mod_perl handler (handler code).

  • Person.t: for this module some test scripts. By default, it's just checking module loaded, you can add some new test unit.

  • t /: test file

  • lib /: actual source storage directory

You can use tar (Linux on) command to the above directory packaged as Person.tar.gz.

Install Perl modules

We can justPerson.tar.gz compressed file to decompress the installation, perform the following steps:

tar xvfz Person.tar.gz
cd Person
perl Makefile.PL
make install

First run "perl Makefile.PL" in the current directory generated Makefile;

Then run "make" to compile and create the required library file;

Followed by "make test" test compile the results are correct; the last run "make install" to install the library files to the system directory, thus ending the entire compilation process.