Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
My quest of creating a clean, strucutured, OOP-good set of programs and modules continues.

So far I have nicely abstracted my handling of various object types into seperate modules (each of which inherit most of their stuff from a superclass - less work for me so it's all good). I have also abstracted out the database layer into a seperate Backend module. Also good.

Now before I start to think about writing some bona-fide applications, I need to think about configuration data. The last thing I ever want to do in my new world of OOP-goodness is hardcode in directory paths and database access credentials. I'd rather eat a copy of Windows XP Home.

So now I'm puzzling about how to get data from a config file into my programs. Some criteria:
  • Configuration data needs to be available to modules and to the actual program
  • Minimize the amount of work needed to 'config-ize' a module or program
Note I'm not interested in the actual config data format at this stage, just how to make it available to everything.

My best idea so far is to have a Config package which reads the config file during a class constructer. Then the first time my Config package gets used, the data is read in, otherwise it's unnecessary.

Then, provide Class Methods to get at the config data. Probably simplest is an AUTOLOAD method which looks up the config values from a hash.

Problems I see with this:

  • Since there is no explicit construction, how do I tell it that I want a different config file during this run?
OK, so that idea bites pretty hard. Going on with this confessional debugging, lets try mark II:

  • Any module/program that might need a config item does something like:

    use Config; my $config = Config->new(); print "Root path is " . $config->root_path;

The key here being that if the Config module already 'knows' that is has an existing config object from a previous call, it will just pass that one back to the caller, so all modules/programs get the same data, and the config file need to be read only once.

In effect, each Config->new() call will merely get back the same reference to the single object, it is held in the Class and only created during the first Config->new call.

The other nice thing about this is that the new method could be modified so a config file override parameter could be passed in. So my user program can use that, and my called modules don't know the difference.

Flaws? Criticism? Pizza?


In reply to Program configuration in the OOP world by Tardis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-19 16:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found