Perl use vs. require usage
Contrast (i) Description:
Both of these functions are a means of loading and referencing Perl's modules, or subroutines,
The difference is that Perl use is found in the current default, and once the module is not in the specified area, it cannot be introduced using Perl
First, the name introduced by Perl use does not require a suffix name, and require requires
Second, Perl use statements are introduced at compile time, and require are introduced at runtime
Third, Perl use introduces modules as well as module modules. and require can not be introduced, to be in a new declaration
Perl Use my
My contains a module.pm
and require needs to introduce Requiremy requiremy::module.pm
If you don't want Perl use to introduce MODULE.PM, you can add a pair of parentheses to the Perl Userequire () to illustrate that.
The introduction of Perl use of the module is a trend, but it can not be said that there is no need for require, depending on the specific circumstances.
Contrast (ii) description
Difference One: Unlike require, Perl use can only be used for module inclusion, the. PM File.
Like what:
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 will be the path separator, equivalent to under UNIX or under Windows. Such as:
Perl Usemydirectory::mymodule;
The compiler will look 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 two: Two are included, but the conditions contained are not the same. The require contains the runtime of the program, and the Perl use inclusion occurs at compile time. It is easy to understand people who have studied C + +.
The following example is wrong:
Copy Code code as follows:
Perl Useconfig;
if ($Config {' osname '}eq "MSWin32")
{Perl usewin32module; }
else {Perl useunixmodule; }
Because Perl use occurs at compile time, the code is not executed, so the value of the $config variable is not judged. Internally, Perl use actually invokes the Require and import static functions. The import () function tells the package which features are to be imported into the current package, which means that you do not have to verify that the function or variable is legitimate before you use it. The Require does not invoke import ().