1. do:
1) form:
Do 'filename ';
Note:
Here, single quotes must be added to filename; otherwise, an error will occur;
Filename can be any suffix or even has no suffix. Do not ask for pl or pm.
2) do:
Do 'filename'
First, you must read the filename file. (If the read fails, undef is returned and $ is set! Variable );
If the read is successful, then compile the statements read by filename (if compilation fails or compilation is incorrect, undef is returned and the error message is set to $ @ variable );
If the compilation is successful, do executes the statement in filename and returns the value of the most expression.
To briefly express do 'filename', you can load all the text in filename to the current file.
3) understand do usage:
A. Split the file:
Main. pl:
Copy codeThe Code is as follows: use strict;
Do 'secret'; # the file can be named with any suffix or even without a suffix;
Seperate:
Print "Hello from seperate file! :-)";
B. You can define a function in seperate and then call it in the current file:
Main. pl
Copy codeThe Code is as follows :#! /Usr/bin/perl
Use strict;
Do 'secret ';
Show ();
Seperate:
Sub show {
Print "Hello from seperate file! :-)";
}
C. You can define the package in seperate and then call the following in the current file:
Main. pl
Copy codeThe Code is as follows :#! /Usr/bin/perl
Use strict;
Do 'secret ';
Show: show_sentence ();
Seperate:
Package Show;
Sub show_sentence (){
Print "Hello from seperate file! :-)";
}
1;
_ END __
# Text is not required
The component name must be the same as the package name, And the seperate does not require the pm suffix.
From the above example, it is easy to obtain that you can use do to conveniently implement file inclusion.
More see http://perldoc.perl.org/functions/do.html
2. require
See http://perldoc.perl.org/functions/require.html
1) form:
Require 'filename ';
Require "filename ";
The two are the same, and they are similar to do;
Require Module;
If no single or double quotation marks are added, the Module will be parsed into the Perl Module, namely, the. pm file, and then search for the Module. pm file in @ INC Array. First, search for the Module in the current directory. pm file (User-Defined). If Perl cannot be found again (@ INC contains: C:/Perl/site/lib C:/Perl/lib .) search.
If the Module contains:, such as require Foo: Bar;, it is replaced with Foo/Bar. pm.
2) explanation of the use of require:
If you use require 'filename' or require "filename" to include files, the method is exactly the same as do;
If you use require Module, You need to define the Module. pm file and the package in the file should be named as Module.
Main. pl
Copy codeThe Code is as follows :#! C: \ perl \ bin \ inperl-w
Use strict;
Require Show;
Show: show_header ();
Show. pm
# Show. pm
Copy codeThe Code is as follows: package Show;
Sub show_header (){
Print "This is the header! ";
Return 0;
}
Sub show_footer (){
Print "This is the footer! ";
Return 0;
}
1;
_ END __
3. use
See http://perldoc.perl.org/functions/use.html
1) form:
Use Module;
Use can only use modules. Similar to require, it requires a Module. pm file, and the package in the file needs to be named by the Module.
Main. pl
Copy codeThe Code is as follows :#! C: \ perl \ bin \ perl-w
Use strict;
Use Show;
Show: show_header ();
Show. pm
Copy codeThe Code is as follows: # Show. pm
Package Show;
Sub show_header (){
Print "This is the header! ";
Return 0;
}
Sub show_footer (){
Print "This is the footer! ";
Return 0;
}
1;
_ END __
2) Difference between require and use:
Require:
Do the things at run time; (load during runtime)
Use:
Do the things at compile time; (load during compilation)
4. perlmod-Perl modules (packages)
Reference http://perldoc.perl.org/perlmod.html
1) Example:
Copy codeThe Code is as follows: # Show. pm
Package Show;
Sub show_header (){
Print "This is the header! /N ";
Return 0;
}
Sub show_footer (){
Print "This is the footer! /N ";
Return 0;
}
1;
_ END __
2)
Generally, the file name must be the same as the package name. Here, the file name is Show;
Variables and functions can be defined;
Do not forget 1;
And the last _ END __
3)
When using require or use modules in other files:
Copy codeThe Code is as follows: use Show;
# Require Show;
Show: show_header ();
5. Perl Function Definition and calling:
Copy codeThe Code is as follows: sub fun_name (){
#...
}
1) if you define fun_name ();
2) If it is defined after use, use & fun_name (); To call the function before use.
6.
Summary:
To sum up, file inclusion can improve code reusability. in Perl, there are two ways to implement File Inclusion:
1) Use do or require (with quotation marks;
2) use the require Module or use Module;
Both.