Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Inter-package communication

by ton (Friar)
on May 09, 2001 at 01:24 UTC ( [id://78967]=perlquestion: print w/replies, xml ) Need Help??

ton has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks,

I have a fairly large Perl application for which I need to reduce the memory size (program is currently at 10M/process on my Win32 box; multiple processes may run the program). This program is naturally broken down into several packages, all of which are 'use'd at the top of the starting Perl file; e.g.

use Alpha; use Beta; use Gamma; use Delta;
Of course, the Alpha package itself 'use's a bunch of other packages, which in turn use other packages, etc. So Alpha might begin with something like
package Alpha; use Rho; use Sigma; use Tau; use Delta;
However, a lot of these packages are never used, depending on the many arguments that the user can pass in at runtime. For instance, Alpha and Beta are mutually exclusive. I thus replaced the first section of code with something like
use Gamma; use Delta; # Argument parsing here if ([user specified Beta]) { require Beta; import Beta; # make Beta calls } else { require Alpha; import Alpha; # make Alpha calls }
This save me around a half meg of RAM. Coolness. I'd like to be able to employ similar logic on all my packages. I thus need for any other package required by my program to be able to access the hash in the main namespace, which is where those command line options are currently being stored. Does anyone know of a way to do this, or another approach I can use to solve this problem? I've considered shoving the data into %INC or the like, but this seems pretty hacky and fairly error-prone.

Any help would be appreciated.

-Ton
-----
Be bloody, bold, and resolute; laugh to scorn
The power of man...

Replies are listed 'Best First'.
Re: Inter-package communication
by arturo (Vicar) on May 09, 2001 at 01:33 UTC

    If I understand what you want to do correctly, then all you'd need to know is that variables that live in a given package's symbol table (i.e. not those declared with my) are available to other packages as long as their names are fully qualified. So you could give your configuration hash a distinctive name, and put it in the main package, and access it like so:

    $main::_my_configuration_hash{$foo}

    I also tentatively suggest you check out the autouse pragma (perldoc autouse) to see if that might be appropriate. It postpones the loading of modules until they're actually used.

      The $main:: was exactly what I needed. Thanks!

      -Ton
      -----
      Be bloody, bold, and resolute; laugh to scorn
      The power of man...

Re: Inter-package communication
by stephen (Priest) on May 09, 2001 at 03:16 UTC
    Since you're doing this to try to reduce your memory footprint, you might want to use AutoLoader. (I'm assuming that these are your own modules that you're loading. If they're someone else's and you can't change them, this won't work.) AutoLoader would enable you to split your modules' code up into small subfiles, and only load in the functions that you need, on-demand.

    That way, you could simply have all of your 'use' statements, but the actual loading would take place only when the modules were actually needed. Your code would thus be more maintainable, since every time you needed to add function calls, you wouldn't need to worry about "Is Alpha loaded in this case? Is Beta?". Plus, you might save some additional memory, since anything not used wouldn't be loaded at all.

    stephen

Log In?
Username:
Password:

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

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

    No recent polls found