in reply to Re^2: Sharing Namespaces
in thread Sharing Namespaces

Yes, on use/require perl checks whether $INC{<modulepath>} exists (<modulepath> being e.g. "My/Module/Namespace") and if so does not attempt to load the module again. This behaviour can be conveniently exploited when mocking modules (see chromatic's MockObject tutorial for more on this).

Update: note, this does not mean that the original module's code only gets run once, so e.g. if your Config module loads a configuration file it will still reload that file every time the module is called. It just means that the module's .pm file only gets loaded once and the code isn't duplicated in memory.


There are ten types of people: those that understand binary and those that don't.

Replies are listed 'Best First'.
Re^4: Sharing Namespaces
by ysth (Canon) on Jan 27, 2006 at 11:00 UTC
    if your Config module loads a configuration file it will still reload that file every time the module is called
    I don't think that's the clearest way to put that. When a module is "use"d, it's import() routine (if any) is normally called, but modules themselves aren't "called". In this case, whether the configuration file is loaded more than once depends on where exactly the loading takes place. If it is done in a subroutine called by each using module (including import()), then the config file will be loaded each time. If, on the other hand, the loading is done by the main code of the Config module, it won't, since that code is only run the first time the module is used. My guess is that the latter is the case.
Re^4: Sharing Namespaces
by Anonymous Monk on Jan 29, 2006 at 00:29 UTC
    One of us is confused here. Use / Require most definatly mean the code will only get run once. (assuming the code you're talking about is not in the import() sub)