First, require function

May divide with the require function the procedure Cheng Duoge the document and founds the function storehouse. For example, has in myfile.pl defines the good Perl function, available sentence require (“myfile.pl”); Contains in the procedure. When the Perl interpreter sees this sentence, in built-in ray variable @INC assigns in the table of contents seeks for document myfile.pl. If has found, in this document’s sentence is carried out, otherwise program termination and output error information:

Can’t find myfile.pl in @INC

As the subroutine call parameter, in the document the last expression’s value becomes the returns value, the require function examined whether it is zero, if is zero terminates. For example the myfile.pl final sentence is:

print (“hello, world! n”);
$var = 0;

Because the final sentence value is zero, the Perl interpreter outputs the following error message and promotes:

myfile.pl did not reture true value

May use the simple variable or the array element and so on transmits the parameter to require, for example:

@reqlist = (“file1.pl”, “file2.pl”, “file3.pl€)
require ($reqlist[$0]);
require ($reqlist[$1]);
require ($reqlist[$2]);

May also not assign the filename, namely:
require;

By now, the variable $ _ value namely transmitted as the filename for require.
Note: If in @INC has in many tables of contents to include the identical document, then only then the first bedding bag contains.

1st, require function and library of subroutines

May found with the require function may use in all Perl procedure the library of subroutines, the step is as follows:

a, determination storage library of subroutines table of contents
b, puts the subroutine extraction to the independent document, puts the document to the library of subroutines table of contents
end of c, each document adds a non-zero value the sentence, the simple the means are the sentence 1;
d, contains the document which in the master routine with require or many institutes need.
when e, movement master routine, refers to the stator library of subroutines table of contents with the – I option, or before transferring require to increase this table of contents to the @INC array.

For example: In supposition table of contents /u/perldir has your Perl library of subroutines, the subroutine mysub storage in document mysub.pl. Now contains this document:
unshift (@INC, “/u/perldir”);
require (“mysub.plâ);

Increases to the unshift transfer table of contents /u/perldir to the @INC array, contains to the require transfer the mysub.pl document’s content takes the procedure a part.

Attention:
1st, should use unshift to come to increase the table of contents to @INC, but is not push. Because push increases to @INC end, then this table of contents will be searched for finally.
2nd, if in your storehouse filename and /usr/local/lib/perl some document of the same name, then the bedding bag will not contain, because require will only contain the document in of the same name first.

2nd, assigns the Perl edition with require

Perl in 5, may use the require sentence to assign the Perl edition which the program run needs. When the Perl interpreter sees the require heel digit, then when its edition is higher than or is equal to this digit only then moves this procedure. For example, below sentence indicates only then the Perl interpreter is 5.001 edition or Gao Shi only then moves this procedure:
require 5.001;

Second, package

The Perl procedure stores the variable and the subroutine name to the symbol table, in the perl symbol table the name set is called wraps (package).

1st, package of definition

May define many packages in a procedure, each Bao You a independent symbol table, the definition grammar is:
package mypack;

This sentence defines named mypack the package, defines from now all variables and the subroutine name stores in this package connection symbol table, until meets another package sentence.

Each symbol table has its own group of variables, subroutine, each group of names are non-correlated, therefore may use same variable in the different package, what but represents is the different variable. For example:

$var = 14;
package mypack;
$var = 6;

First sentence foundation variable $var and stores in the main symbol table, the third sentence founds another variable $var of the same name and stores in a mypack package of symbol table.

2nd, in passenger compartment cut

May momentarily back and forth cut in the procedure in the passenger compartment, for example:

1: #! /usr/local/bin/perl
2:
3: package pack1;
4: $var = 26;
5: package pack2;
6: $var = 34;
7: package pacЯ
8: print (“$varn”);

The movement result is as follows:

$ program
26
$

The third line defined has wrapped pack1, fourth line of foundation variable $var, stored in wraps pack1 in the symbol table, the fifth line of definition wrapped pack2 newly, the sixth line founded another variable $var, stored in wraps pack2 in the symbol table. This two independent $var, separately stores in the different package. The seventh line also assigns pack1 is the current package, because wrapped pack1 already to define, like this, all variables and the subroutine definition and the transfer the name which stored for in this package’s symbol table. Therefore the eight columns to the $var transfer is pack1 package of $var, its value is 26.

3rd, main package

The storage variable and the subroutine name’s default symbol table is and the named main package is connected. If has defined other packages in the procedure, when you want to cut the use default symbol table, may the reassign main package:
package main

Thus, the procedure then probably from has not defined a package of dissimilarity, the variable and the subroutine name likely usual such storage.

4th, package of quotation

May quote in other package’s variable or the subroutine in a package, the method is adds on Bao Ming and single quotes in front of the variable name, for example:

package mypack;
$var = 26;
package main;
print (“$mypack’varn”);

Here, $mypack’var is mypack package of variable $var.
Attention: In Perl in 5, Bao Ming and the variable name separate with the double colon, namely $mypack::var. The single quotes quotation’s way still supported, but in future edition will support not necessarily.

5th, assigns not current package

In Perl in 5, may use the following sentence to assign not current package:
package;

By now, all variables must point out the respective package name explicitly, otherwise invalid–Wrong.
$mypack::var = 21; #ok
$var = 21; #error – no current package

This kind of situation until uses the package sentence to assign the current package.

6th, package and subroutine

A package of definition affects the procedure in all sentences, including subroutine, for example:

package mypack;
subroutine mysub {
local ($myvar);
# stuff goes here
}

Here, mysub and myvar are wrap mypack a part. In wraps outside mypack to transfer subroutine mysub, then must assign the package: $mypack’mysub.
May cut the package in the subroutine:

package pack1
subroutine mysub {
$var1 = 1;
package pack2;
$var1 = 2;
}

This section of codes founded two variable $var1, in wraps in pack1, in wraps in pacа, in package’s confined variable can only in its definition sentence blocks and so on subroutine use, the ordinary confined variable is likely same.

7th, defines the private data with the package

A package of most usual use is uses, in includes in the global variable document wh
ich the subroutine and the subroutine use, defines such package as the subroutine, may guarantee that the subroutine use the global variable cannot use in other places, such data is the private data. The further, may guarantee Bao Ming not to be possible in other place use. Private data example:

1: package privpack;
2: $valtoprint = 46;
3:
4: package main;
5: # This function is the link to the outside world.
6: sub printval {
7: &privpack’printval();
8: }
9:
10: package privpack;
11: sub printval {
12: print (“$valtoprintn”);
13: }
14:
15: package main;
16:1; # return value for require

This subroutine only then after transferring printval can have the output.
This document divides into two parts: With outside contacting part and private part. The former for the default main package, the latter to wrap privpack. 6~8th line of definition’s subroutine printval may by other procedures or the subroutine call. printval the output variable $valtoprint value, this variable only in wraps in privpack the definition and the use. The 15th, 16 lines guaranteed that it contains after the require sentence is worked normally by other procedures, 15 about to current packages establish default wrap the main,16 line of returns non-zero value to cause require not to report wrong.

8th, package and system variable

Even if the following variable transfers from other packages, also has an effect in the main package:

Document variable STDIN, STDOUT, STDERR and ARGV
Variable %ENV, %INC, @INC, $ARGV and @ARGV
Other include the special character system variable

9th, visit symbol table

Searches symbol table available array %_package in the procedure, here package is symbol table respective Bao Ming who wants to visit. For example %_main includes the default symbol table.
Usually does not need to search the symbol table personally.

Third, module

The most large-scale procedures divide Cheng Duoge the part, each part usually includes or many sub-procedures and the related variable, carries out specific or many tasks. Has gathered the variable and the subroutine part is called the program module.

1st, foundation module

Perl in 5 will found the module with the package, the method is founds Bao in Bing the existence document of the same name. For example, the named Mymodult package storage (extension .pm expresses Perl Module) in document Mymodult.pm. Module Mymodult as follows includes subroutine myfunc1 and myfunc2 and variable $myvar1 and $myvar2.

1: #! /usr/local/bin/perl
2:
3: package Mymodule;
4: require Exporter;
5: @ISA = qw(Exporter);
6: @EXPORT = qw (myfunc1 myfunc2);
7: @EXPORT_OK = qw ($myvar1 $myvar2);
8:
9: sub myfunc1 {
10: $myvar1 += 1;
11: }
12:
13: sub myfunc2 {
14: $myvar2 += 2;
15: }

The 3~7th line is the standard Perl module definition mode. The 3rd line of definition package, the 4th luggage including built-in Perl module Exporterƾ, 7 marches forward the displeasing person procedure and the variable output by relates with the outside. The 6th line of foundation named @EXPORT special array, in this array’s subroutine may transfer by other procedures, here, myfunc1 and myfunc2 may visit. Other any defines in the module but has not bestowed on for the array @EXPORT subroutine is private, can only in the module interior transfer. The 7th line founds another named @EXPORT_OK the special array, includes may by the exterior procedure visit variable, here includes $myvar1 and $myvar2.

2nd, inducts the module

Inducts in your Perl procedure the module to use the use sentence, the following sentence inducted the Mymodule module:
use Mymodule;

Thus, in the module Mymodule subroutine and the variable might use.

Cancels inducts the module to use the no sentence, the following sentence has cancelled Mymodule module induction:
no Mymodule;

Below looked that the example which inducts the module and cancels inducts, uses the integer module request all digital operation based on the integer, the floating number is transformed before the operation as the integer.

1: #! /usr/local/bin/perl
2:
3: use integer;
4: $result = 2.4 + 2.4;
5: print (“$resultn”)
6:
7: no integer;
8: $result = 2.4 + 2.4;
9: print (“$resultn”);

The program output is as follows:

$ program
4
4.8
$

If use or the no sentence appears in the sentence block, then only has an effect in this block’s range of validity, for example:

use integer;
$result1 = 2.4 + 2.4;
if ($result1 == 4) {
no integer;
$result2 = 3.4 + 3.4;
}
$result3 = 4.4 + 4.4;

The result output is as follows:

4
6.8
8

Here, no sentence only in the if sentence effective, left the if sentence still to use the integer module, therefore 4.4 before making the addition to transform 4.

3rd, pre-definition module

PerlŅ have provided many useful pre-definition modules, may use use to induct cancels with the no sentence. Below is in the storehouse the most useful some modules:

the integer use integer operates
Diagnostics Output many diagnostic messages (warning)
The English permission English name serves as the system variable the alias
Env inducts the environment variable the Perl module
POSIX POSIX standard (IEEE 1003.1) Perl connection
Socket loads the C language the sleeve joint character processing mechanism

In the Perl documents has the complete pre-definition module tabulation.
Note: The world’s the Perl 5 users have written many useful modules, CPAN (Comprehensive Perl Archive Network) the Perl documents have its complete tabulation. See its website about CPAN more information:
http://www.perl.com/perl/CPAN/README.html.