Comparison and Analysis of use and require usage in Perl

Source: Internet
Author: User

Comparison of Perl use and require usage

Comparison (1) Description:
Both functions mean loading and referencing Perl modules, or sub-functions.Program,

The difference is that Perl use is searched in the current default. Once the module is not in the specified region, it cannot be introduced with Perl use.

First, the names introduced by Perl use do not need to be suffixed, while require requires
2. Perl use statements are introduced during compilation, and require statements are introduced during runtime.
Third, while introducing modules, Perl use also introduces modules. Require cannot be introduced, and it must be declared again

Perl use my

My contains a module. PM

While require needs to introduce requiremy: module. PM
If you do not want Perl use to introduce module. pm, you can use Perl userequire () to add a pair of parentheses.
Fourth, using Perl use to introduce modules is a trend, but it cannot be said that require does not exist, depending on the specific situation.

Comparison (ii) Description

Difference 1: Unlike require, Perl use can only be used for module inclusion, that is, the. PM file.
For example:
Perl usemymodule;
In fact, the compiler will look for the mymodule. PM module from the directory specified by @ INC. If the module name contains: Double colon, the double colon serves as the path separator, which is equivalent to/in Unix or windows. For example:
Perl usemydirectory: mymodule;
The compiler looks for the mymodule module from the mydirectory subdirectory in the directory specified by @ INC, similar to the following path:
C: \ Perl \ Lib \ mydirectory
C: \ Perl \ ext \ Lib \ mydirectory
C: \ Perl \ site \ Lib \ mydirectory

Difference 2: both are contained, but the conditions are different. Require inclusion occurs during the program running period, while Perl use inclusion occurs during the compilation period. Those who have studied C/C ++ are easy to understand.
The following example is incorrect:

CopyCode The Code is as follows: Perl useconfig;
If ($ config {'osname'} EQ "mswin32 ")
{Perl usewin32module ;}
Else {Perl useunixmodule ;}

because Perl use occurs during compilation and the code is not executed, the value of the $ config variable cannot be determined. Internally, Perl use actually calls the require and import static functions. The import () function tells the package which features will be imported into the current package, which means you do not have to verify whether the function or variable is valid before using it. Require does not call import.

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.