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.