Friday, September 17, 2010

Difference between use and require

A use anywhere in the code will be evaluated when the code is run compiled, but require - import's can only get evaluated when encoutered - good for when you want one module or another but both have quite a large initialisation overhead which means you only want the one you need.

The use statement works at compile time, require at run time. So if you have a module that does a lot in a begin block and you don't really need it in all cases, then it's clever to "require" it there where you want to use it but don't "use" it. So you don't have to wait for all that initializations of that module in case you don't need it.

When you 'use' the control is passed to that file at runtime.
Whereas, when you 'require' the code from that file is included into your code file at run time. (Remember, 'perl' is an interpreter, not compiler).


Use :
1. The method is used only for the modules(only to include .pm type file)
2. The included objects are verified at the time of compilation.
3. No Need to give file extension.
Require:
1. The method is used for both libraries and modules.
2. The included objects are verified at the run time.
3. Need to give file Extension.

 
use myModule;

or

require "myModule.pm";

No comments:

Post a Comment