Perl Packages Modules
Each package in Perl has a separate symbol table, the definition syntax is:
package mypack;
This statement defines a package named **mypack**. All variables and subroutine names defined after this are stored in the symbol table associated with this package until another **package** statement is encountered.
Each symbol table has its own set of variable and subroutine names, and each group of names is unrelated. Therefore, you can use the same variable name in different packages, but they represent different variables.
To access a variable in another package, you can specify it using "package name + double colon (::) + variable name".
The default symbol table for storing variable and subroutine names is associated with the package named **main**. If you define other packages in your program and want to switch back to use the default symbol table, you can re-specify the main package:
package main;
In this way, the subsequent program is as if no package had ever been defined, and variable and subroutine names are stored as usual.
In the following example, the file has main and Foo packages. The special variable **__PACKAGE__** is used to output the package name:
## Example
#!/usr/bin/perl$i = 1; print"Package Name : " , __PACKAGE__ , "$in"; package Foo; $i = 10; print"Package Name : " , __PACKAGE__ , "$in"; package main; $i = 100; print"Package Name : " , __PACKAGE__ , "$in"; print"Package Name: " , __PACKAGE__ , "$Foo::in"; 1;
Execute the above program, the output is:
Package Name : main 1Package Name : Foo 10Package Name : main 100Package Name: main 10
* * *
## BEGIN and END Blocks
Perl provides two keywords: BEGIN and END. They can each contain a set of scripts to be executed before or after the program body runs.
Syntax format:
BEGIN { ... }END { ... }BEGIN { ... }END { ... }
* Each **BEGIN** block is executed after the Perl script is loaded and compiled but before any other statements are executed.
* Each **END** block is executed before the interpreter exits.
* **BEGIN** and **END** blocks are particularly useful when creating Perl modules.
If you don't quite understand yet, let's look at an example:
## Example
#!/usr/bin/perl package Foo; print"Begin and Block Examplen"; BEGIN{print"This is BEGIN blockn"}END{print"This is END blockn"}1;
Execute the above program, the output is:
This is BEGIN blockBegin and Block ExampleThis is END block
* * *
## What is a Perl Module?
Perl packages are used to create modules in Perl5.
A Perl module is a reusable package. The module name is the same as the package name, and the defined file has the **.pm** suffix.
Below we define a module Foo.pm, the code is as follows:
## Example
#!/usr/bin/perl package Foo; sub bar{print"Hello $_n"}sub blot{print"World $_n"}1;
The following points need to be noted about modules in Perl:
* The functions **require** and **use** are used to load a module.
* **@INC** is a special array built into Perl that contains directory paths pointing to where the library routines are located.
* The **require** and **use** functions call the **eval** function to execute code.
* The trailing **1;** returns TRUE, which is required, otherwise an error is returned.
* * *
## Require and Use Functions
Modules can be called through the **require** function, as follows:
## Example
#!/usr/bin/perl require Foo; Foo::bar("a"); Foo::blot("b");
They can also be referenced through the **use** function:
## Example
#!/usr/bin/perl use Foo; bar("a"); blot("b");
We notice that require references need to use the package name to specify the function, while use does not. The main differences between the two are:
* 1γrequire is used to load modules or perl programs (.pm suffix can be omitted, but .pl must have it)
* 2γPerl use statement is introduced at compile time, require is introduced at runtime
* 3γPerl use introduces the module as well as its submodules. While require cannot introduce them, they need to be declared again
* 4γUSE searches within the current default @INC, once the module is not in @INC, USE cannot introduce it, but require can specify a path
* 5γWhen USE references a module, if the module name contains ::
YouTip