Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: Packaging up my code sensibly

by biosysadmin (Deacon)
on Jun 12, 2005 at 16:11 UTC ( #465974=note: print w/replies, xml ) Need Help??

in reply to Packaging up my code sensibly

For your first point, you should read this thread on code reuse and method sharing, there are several ideas that are discussed for sharing objects through utility classes and superclasses.

For your second point, why not just have a MyPackage::Config file? Just something simple like this should work:

use MyPackage::Config; my $cgi_url = MyPackage::Config->cgi_url;
On the one had it may seem like overkill to make a module just for just reading config files, but I've done that before and I've found that abstracting out the reading/writing of configuration files makes your program read well. It also separates your configuration file format out, so that if you ever decide to change from a require'd file, then you only have to change that in one place. I personally think that requiring or eval'ing code in that manner is a bad idea, but that's a separate discussion.

Even better, once you make that configuration file object, you can share it using whatever method you use to share HTML::Template objects from your first question. :)

I actually think your third question may be answered in the linked thread as well. That thread is actually more pertinent to this question than the first.

Replies are listed 'Best First'.
Re^2: Packaging up my code sensibly
by punkish (Priest) on Jun 12, 2005 at 16:23 UTC
    Thanks for the reply. Your linked thread is quite specific to CGI::App, and while I learned a little bit from it, I am still in the confused land.

    Wrt reading a conf file, that is not really a problem. I am already reading an external conf file. I can also use Config::Simple or its many other brethren. However, the problem more is how to share these values among the various imported modules. I guess, that is really what I need a tutorial on.

    I am not really share my H::T objects right now. I have only one module that I bring in. I create an H::T object in it. However, if I chop up into many sub-packages, I will have to schlep H::T object back and forth. I am not quite sure how to do that correctly.


    when small people start casting long shadows, it is time to go to bed
      Isn't what you need is a singleton object to store your values? Something that you'll call like this :
      use MyModules::Config; # here's the singleton magic: # "new" recalls the existing instance if any. my $conf=Config->new();
      from each script. And the Config package would look like this : This way, all your modules can share the same config without passing any parameter around : just call my $conf=Config->new() from each module, the first to call it will load the configuration data, the others will simply share that instance.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2020-06-02 02:55 GMT
Find Nodes?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?

    Results (12 votes). Check out past polls.