Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re^2: Sharing Namespaces

by skazat (Chaplain)
on Jan 27, 2006 at 09:17 UTC ( #525926=note: print w/replies, xml ) Need Help??

in reply to Re: Sharing Namespaces
in thread Sharing Namespaces

This is basically what I'm doing - but let me get something straight -

Perl will not use or require the same file twice without some prodding, so you're not saving anything in your attempted optimization.

Does that mean, that under the hood, Perl is only loading up the code in say, once and every other call is just being directed to the address in memory where this code is?

Cause that would make my problem moot.


-justin simoni
skazat me

Replies are listed 'Best First'.
Re^3: Sharing Namespaces
by tirwhan (Abbot) on Jan 27, 2006 at 09:30 UTC

    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.
      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.
      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)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://525926]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2022-05-16 19:59 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (63 votes). Check out past polls.