Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Sharing Namespaces

by duff (Parson)
on Jan 27, 2006 at 07:35 UTC ( [id://525921]=note: print w/replies, xml ) Need Help??


in reply to Sharing Namespaces

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

Aside from the excellent advice to use Exporter, if your Config.pm file (not a good name BTW) has a package declaration and the variables are package variables, then you can access those variables by their full name (i.e. $Config::foobar) from within your main program and any modules that use your module. It sounds like this is what you're doing, but I wanted to be sure.

Replies are listed 'Best First'.
Re^2: Sharing Namespaces
by skazat (Chaplain) on Jan 27, 2006 at 09:17 UTC

    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, Config.pm 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

      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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-23 23:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found