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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

require vs use

Status
Not open for further replies.
my understanding is as follows...

Require is used for including other scripts into your current script, and I may be wrong but I beleive that will also mean they run in the same package space.

Use is for using modules, and work in a more OO environment and usually in their own package space also.

Even if i have a global var script, I prefer to roll it into my own module and use it that way, you can define global vars with OUR which can be used cross-package space.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
I agree with what you said, 1DMF.

But with regarding efficiency, which one is better, use or require?
 
dunno, never been an issue, I used to use 'require', now I use 'use'.

<--- calls for an expert to answers



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
I don't think there is much of an efficieny concern here. The basic difference is that "use" occurs at compile time and "require" occurs at run time. If you read the "use" page it says:

use Module VERSION LIST

use Module VERSION
use Module LIST
use Module
use VERSION

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.


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Complie time and runtime here are... a bit misleading. It's an interpreted language and gets translated into machine readable stuff each time you run it.

Two stages though

- 1 From Perl into p-code
- 2 Excecute p-code

So when we use the word compile we mean stage 1, and it happens each time the script is run.

The rather obvious answer is - sod run-time efficiency. What we're after here is developer efficiency and code we won't have to fiddle with in five years when the language changes.

More efficient to use 'use', because then you won't get a phone call in a few years "you know those perl scripts you worked on in '09?"

Mike

 
Mike's right. Code it the way that is simplest and cleanest to understand. Afterwards, if you actually have a performance problem, consider recoding it. Even then, comparing the cost of programmer time vs. the cost of hardware, in a commercial environment it may still be cheaper to buy a faster box...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Thank you all for your inputs!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top