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

4

31

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

Standstill answered 21/7, 2009 at 20:39 Comment(2)
@Ether: but this is the m. One of them, anyway.Ahmad
This may be easy to figure out on your own, but it's definitely a very good Perl question that beginners are prone to ask. I don't see why it should not be on stackoverflow. Where's the harm?Bott
P
30

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;
}
Ploughboy answered 21/7, 2009 at 20:54 Comment(1)
@Inshalla I like to leave a little wiggle room for my faulty memory.Ploughboy
A
18

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.

Alis answered 21/7, 2009 at 20:42 Comment(0)
P
10

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.

Philanthropy answered 21/7, 2009 at 21:1 Comment(2)
In addition to the wonderful perldoc command, you can access all of the same documentation through perldoc.perl.orgPloughboy
Ah. Your kbd tags make it harder to tell what's a link (as well as rendering not very prettily).Ahmad
S
7

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

Statfarad answered 21/7, 2009 at 20:42 Comment(2)
Don't forget the calling of import.Ploughboy
This is the answer they are looking for on Perl tests and job interviews. You need to memorize this!Cooler

© 2022 - 2024 — McMap. All rights reserved.