FAQ 8.46 What's the difference between require and use?

PerlFAQ Server

Usenet Archive
Joined
Oct 7, 2004
Messages
807
Reaction score
0
This is an excerpt from the latest version perlfaq8.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org .

8.46: What's the difference between require and use?

(contributed by brian d foy)

Perl runs "require" statement at run-time. Once Perl loads, compiles, and runs the file, it doesn't do anything else. The "use" statement is the same as a "require" run at compile-time, but Perl also calls the "import" method for the loaded package. These two are the same:

use MODULE qw(import list);

BEGIN {
require MODULE;
MODULE->import(import list);
}

However, you can suppress the "import" by using an explicit, empty import list. Both of these still happen at compile-time:

use MODULE ;

BEGIN {
require MODULE;
}

Since "use" will also call the "import" method, the actual value for "MODULE" must be a bareword. That is, "use" cannot load files by name, although "require" can:

require "$ENV{HOME}/lib/Foo.pm"; # no @INC searching!

See the entry for "use" in perlfunc for more details.

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They are not necessarily experts in every domain where Perl might show up, so please include as much information as possible and relevant in any corrections. The perlfaq-workers also don't have access to every operating system or platform, so please include relevant details for corrections to examples that do not work on particular platforms. Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in perlfaq.pod.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,049
Messages
3,104,246
Members
248,133
Latest member
DujonT

Latest Threads

Top