Perl programs and modules
Each package in Perl has a separate symbol table with a definition syntax of:
package mypack; Span>
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 is encountered. b> until the statement.
Each symbol table has its own set of variables, subroutine names, and each group name is irrelevant, so you can use the same variable name in different packages, and represent different variables.
Variables that access another package from one package can be specified by "package name + double colon ( :: ) + variable name".The default symbol table for storing the names of variables and subroutines is associated with a package named main. If you have other packages defined in the program, you can re-specify the main package when you want to switch back to using the default symbol table:
package main; Span>
This way, the next program seems to have never defined a package, and the names of variables and subroutines are stored as usual.
The files in the following examples have main and Foo packages. Special variable __PACKAGE__ is used to output the package name:
Execute the above program, the output is:
package call : Span> main 1 Package call : Foo 10 Package call : primary 100 Package call: major 10
BEGIN and END modules
The Perl language presents two keywords: BEGIN, END. They can every contain a fixed of scripts for execution before or after the frame of this system.The
syntax is as follows:
BEGIN ... < /span> END ... BEGIN ... END ...
Each BEGIN module is completed after the Perl script is loaded and compiled however earlier than other statements are completed.
Each END statement block is finished before the interpreter exits.
BEGIN and END announcement blocks are specially beneficial while growing Perl modules.
If you don't understand it, we are able to see an example:
Execute the above program, the output is:
This is BEGIN Span> assertion block Begin and Block Instance This is END Statement Blocks
What is a Perl module?
Perl5 makes use of the Perl bundle to create modules.
The Perl module is a reusable package with the equal name because the package name and a described record suffix of .Pm.
The following we've defined a module Foo.Pm, the code is as follows:
The following factors should be noted about modules in Perl:
features require and use will load a module.
@INC is a unique array constructed into Perl that includes a listing direction to the location of the library routine.
require and use capabilities name the eval feature to execute the code.
End 1; Execution returns TRUE, that's required, in any other case an blunders is again.
Require and Use capabilitiesThe
module can be called with the require characteristic as follows:
also can be referenced by way of the use function:
We observed that the require reference desires to apply the package deal name to specify the characteristic, and use does not, the primary distinction among the two is:
- 1, require is used to load the module or perl program (.Pm suffix can be neglected, however .Pl should be)
- 2, the Perl use assertion was added at collect time, and the require turned into added at runtime
- 3. Perl use introduces modules and introduces submodules of modules. And require can not be introduced, to be re-declared
- four, USE is to discover in the contemporary default @INC, as soon as the module isn't in @INC, use USE can not be introduced, however require can specify the direction
- five. When USE refers to the module, if the module name carries :: double colon, the double colon can be used because the course separator, that's equivalent to Unix// under Windows. Such as:
The list symbol can be exported from the module by including the subsequent announcement:
require Exporter; @ISA = qw(Exporter);The
@EXPORT array consists of the names of the variables and capabilities exported via default:
bundle Module; require Exporter ; @ISA = qw(Exporter); @EXPORT = qw(bar blat); # default exported symbols sub bar print "Hello $_n" Span> sub blat print "World $_n" Span> sub splat print "Not $_n" Span> # Not exported ! 1;
Create a Perl module
It's easy to create a Perl module by using distributing the local device h2xs with Perl.
You can kind h2xs in command line mode to see a list of its parameters.
h2xs syntax layout:
$ h2xs -AX < /span>-n ModuleName
-A Ignore the autoload mechanism
-X Ignore XS elements
-n Specify the call of the extension module
For instance, if your module is in a Person.Pm record, use the subsequent command:
$ h2xs -AX < /span>-n Person
Executing the above application will output:
Writing Person/lib/Person< /span>.pm Writing Person /Makefile.PL Writing Person /README Writing Person /t/Person.t Writing Person /Changes Writing Person /MANIFEST
Person listing you could see the newly delivered listing and document description:
README: This record carries a few set up information, module dependencies, copyright statistics, etc.
Changes: This file is used as a changelog record to your undertaking.
Makefile.PL : This is the same old Perl Makefile constructor. Used to create a Makefile.PL report to collect the module.
MANIFEST: This file is used to mechanically build a module version distribution of type tar.Gz. This way you may take your module to CPAN for distribution or distribution to others. It contains a listing of all of the files you have got in this project.
Person.Pm : This is the principle module file, containing your mod_perl handler code.
Person.T : Some check scripts for this module. By default it just exams the loading of the module, you may add some new check gadgets.
t/ : Test report
lib/ : The listing in which the real supply is stored
You can use the tar (on Linux) command to bundle the above directory as Person.Tar.Gz.
Install Perl module
We can unpack the Person.Tar.Gz document that we simply compressed. The steps are as follows:
tar xvfz Person.< /span>tar.gz Cd Person Perl Makefile.PL Make Make set up
First run "perl Makefile.PL" to generate Makefile inside the cutting-edge listing;
Then run "make" to bring together and create the desired library documents;
After the usage of "make check" to check whether the compilation result is accurate; sooner or later run "make install" to put in the library record to the gadget listing, and the complete compilation procedure ends.