Comparative analysis of use and require usage in Perl

Time:2022-5-14

Comparison of Perl use and require usage

Description of comparison (I):
These two functions are the same meaning, loading and referring to Perl modules or subroutines,

The difference is that Perl use is found in the current default. Once the module is not in the specified area, Perl use cannot be introduced

First, the name introduced by Perl use does not need a suffix, but requires
Second, Perl use statements are introduced at compile time and require is introduced at run time
Third, Perl use not only introduces modules, but also introduces sub modules of modules. Require cannot be imported, but must be redeclared

Perl use my

My contains a module pm

And require requires the introduction of requiremy:: module pm
If you don’t want Perl use to introduce module PM can be described with Perl userrequire() plus a pair of parentheses.
Fourth, it is a trend to introduce modules with Perl use, but it can not be said that there is no need for require, depending on the specific situation.

Comparison (II) description

Difference 1: unlike require, Perl use can only be used for module inclusion, that is PM file.
For example:
Perl useMyModule;
In fact, the compiler will look for mymodule from the directory specified by @ Inc PM module. If the module name contains:: Double colon, the double colon will be used as the path separator, which is equivalent to that under UNIX and / or windows. For example:
Perl useMyDirectory::MyModule;
The compiler will find the mymodule module from the subdirectory of mydirectory under 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 included, but the conditions of inclusion are different. The require inclusion occurs at program run time, while the Perl use inclusion occurs at compile time. People who have studied C / C + + are easier to understand.
The following example is wrong:

 

Copy codeThe code is as follows:

Perl useConfig; 
if($Config{‘osname’}eq”MSWin32″)
 {  Perl useWin32Module;  }
 else  {  Perl useUnixModule;  }

 

Because Perl use occurs at compile time 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 that you don’t have to verify whether the function or variable is legal before using it. And require will not call import().

Recommended Today

Vue+Express+Mysql full stack project add, delete, modify, search, sort and export table functions

This article records the implementation of a small full-stack project. The front-end uses the vue framework, the back-end uses the express framework, and the database uses mysql. product demand analysis The product manager said, I need to do a web people management project, the project contains the following functions: Users can create new data on […]