Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Perl, what is the difference between use and require for loading a module?

What is the difference between doing use My::Module and require My::Module?

like image 269
Sam Lee Avatar asked Jul 21 '09 20:07

Sam Lee


People also ask

What is the use of use in Perl?

Note that a use statement is evaluated at compile time. A require statement is evaluated at execution time. If the VERSION argument is present between Module and LIST, then the use will call the VERSION method in class Module with the given version as an argument.

What is require in Perl?

Description. This function then it demands that the script requires the specified version of Perl in order to continue if EXPR is numeric. If EXPR or $_ are not numeric, it assumes that the name is the name of a library file to be included.

What is the difference between use and require in PHP?

Use require when the file is required by the application. Use include when the file is not required and application should continue when file is not found.


4 Answers

The use function:

use ModuleName;

is equivalent to the following code using the require function:

BEGIN {
    require ModuleName;
    ModuleName->import;
}

The BEGIN block causes this code to run as soon as the parser sees it. The require loads the module or dies trying. And then the import function of the module is called. The import function may do all sorts of things, but it is common for it to load functions into the namespace that used it (often with the Exporter module).

It is important to note that import will not be called in this case:

use ModuleName ();

In that case, it is equivalent to

BEGIN {
    require ModuleName;
}
like image 87
Chas. Owens Avatar answered Oct 05 '22 00:10

Chas. Owens


From perldoc -q "difference between require and use":

use Module is like require Module, except that use

4.1: loads the module at compile time, not run-time.

4.2: imports symbols and semantics from that package to the current one.

like image 45
Silfverstrom Avatar answered Oct 04 '22 23:10

Silfverstrom


Perl comes with great documentation. Everyone would benefit from reading the entire documentation at least once every few months.

C:\> perldoc -f require

Otherwise require demands that a library file be included if it hasn't already been included. The file is included via the do-FILE mechanism, which is essentially just a variety of eval with the caveat that lexical variables in the invoking script will be invisible to the included code. Has semantics similar to the following subroutine:

... etc. Similarly,

C:\> perldoc -f use

Imports some semantics into the current package from the named module, generally by aliasing certain subroutine or variable names into your package. It is exactly equivalent to

BEGIN { require Module; Module->import( LIST ); }

except that Module must be a bareword.

... etc

There is also the perlfaq entry although I think it is less informative than the above.

like image 26
Sinan Ünür Avatar answered Oct 04 '22 22:10

Sinan Ünür


use runs at compile time, and require runs at run time.

like image 27
1800 INFORMATION Avatar answered Oct 04 '22 23:10

1800 INFORMATION