Comparative analysis of use and require usage in Perl _perl

Source: Internet
Author: User

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 ().

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.