Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What's the difference between require and use?

Syntax

What's the difference between require and use?

by  MikeLacey  Posted    (Edited  )
Please browse through faq219-2884 and faq219-2889 first. Comments on this FAQ and the General FAQ's are very welcome.

Perl offers several different ways to include code from one file into another. Here are the differences between the various inclusion commands:

1) [tt]do $file[/tt] is like [tt]eval `cat $file`[/tt], except that [tt]do[/tt]
1.1: searches [tt]@INC[/tt] and updates [tt]%INC[/tt].
1.2: gives an *unrelated* lexical scope to the eval'ed code.
2) [tt]require $file[/tt] is like [tt]do $file[/tt], except that [tt]require[/tt]
2.1: checks for redundant loading, skipping already loaded files.
2.2: raises an exception on failure to find, compile, or execute $file.
2.3: It may help, if you're a C programmer, to think of [tt]require[/tt] as being like #include
3) [tt]require Module[/tt] is like [tt]require "Module.pm"[/tt], except that [tt]require[/tt]
3.1: translates each "::" into your system's directory separator.
3.2: primes the parser to disambiguate class Module as an indirect object.
4) [tt]use Module[/tt] is like [tt]require Module[/tt], except that [tt]use[/tt]
4.1: loads the module at compile time, not run-time.
4.2: imports symbols and semantics from that package to the current one.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top