Perl file contains (do,require,use) instruction Introduction _perl

Source: Internet
Author: User
Tags function definition

1. do:

1) Form:
do 'filename';
Explanation:
Here filename needs to be enclosed in single quotes, otherwise an error will occur;
filename can be any suffix, even without suffix, does not require pl or pm, etc.

2) Understanding of do:
do 'filename'
First you need to read in the file named filename (if reading fails, undef is returned and the $! Variable is set);
If the reading is successful, then the statement read by filename is compiled (if it cannot be compiled or a compilation error occurs, it returns undef and sets the error message to the $ @ variable);
If the compilation is also successful, do executes the statement in filename and finally returns the value of the most expression.

The function of short expression do 'filename' is to be able to load all the text in filename into the current file.

3) Understand the usage of do:
a. Split the file:
main.pl:

Copy the code:

use strict;
do 'seperate'; #Files can be named with any suffix or no suffix;
seperate:
print "Hello from seperate file! :-)";
b. Functions can be defined in seperate and then called in the current file:
main.pl

Copy the code:

#! / usr / bin / perl
use strict;
do 'seperate';
show ();
seperate:
sub show {
    print "Hello from seperate file! :-)";
}
c. You can define a package in seperate and then call it in the current file:
main.pl

Copy the code:

#! / usr / bin / perl
use strict;
do 'seperate';
Show :: show_sentence ();
seperate:
package Show;
sub show_sentence () {
    print "Hello from seperate file! :-)";
}
1;
__END__
# 都 没有 文
The filename must be the same as the package name, and seperate does not require a pm suffix.

From the above example, it is easy to get, using do can easily achieve file inclusion.
See more at http://perldoc.perl.org/functions/do.html

2. require
See http://perldoc.perl.org/functions/require.html
1) Form:
require 'filename';
require "filename";
These two are the same, and they are similar to the use of do;

require Module;
If you do not add single or double quotes, the following Module will be parsed into a Perl module, that is, a .pm file, and then search for the Module.pm file according to @INC Array. First search the Module.pm file (user-defined) in the current directory. If you can't find it, go to Perl (@INC contains: C: / Perl / site / lib C: / Perl / lib.)
If :: appears in Module, such as require Foo :: Bar; will be replaced with Foo / Bar.pm

2) Explanation of the use of require:
If you use require 'filename' or require "filename" to include a file, 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 Module.
main.pl

Copy the code:

#! C: \ perl \ bin \ inperl -w
use strict;
require Show;
Show :: show_header ();
Show.pm
# Show.pm

Copy the code:

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, and its usage is similar to require, which requires a Module.pm file, and the package in the file needs to have Module to name the module.
main.pl

Copy the code:

#! C: \ perl \ bin \ perl -w
use strict;
use Show;
Show :: show_header ();
Show.pm

Copy the code:

# 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) The difference between require and use:
require:
do the things at run time;
use:
do the things at compile time;
4. perlmod-Perl modules (packages)
Reference http://perldoc.perl.org/perlmod.html
1) Example:

Copy the code:

# 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)
The general file name needs to be the same as the package name, here is Show;
Can define variables and functions;
Don't forget 1;
And finally __END__

3)
When using require or use modules in other files:

Copy the code:

use Show;
#require Show;
Show :: show_header ();
5. Perl function definition and call:
 

Copy the code:

sub fun_name () {
 # ...
}
1) If the definition is before use, directly fun_name (); when using
2) If the definition is used after use, you need to use & fun_name (); to call the function before.

6.
summary:
In summary, file inclusion can improve the reusability of the code. There are only two ways to implement file inclusion in Perl:
1) Use do or require (quoted) methods;
2) The module mode using require Module or use Module;
Both are acceptable.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.