The Problem:
I have some Perl Modules, each of which has its own config file containing a rather complex data structure. The Modules are structured in a hierarchy and depend quite heavily on inheritance. Now I would like to use inheritance with the config values to e.g. specify a default value in one package and override it in another.
It look sort of like this:
package Parent;
require ('./parent.cfg');
package Parent::Child;
@ISA=('Parent');
require ('./child.cfg');
package Parent::Child2;
@ISA=('Parent');
require ('./child2.cfg');
package Parent::Child::Grandchild;
@ISA=('Child');
require ('./grandchild.cfg');
A config file looks like:
$config={
string=>"bla",
array=>['val1','val2'],
hash=>{
foo=>'bar',
},
# etc, put any valid perl data structure here
};
My not-so-ideal solution:
I wrote a routine (called "config") that returns config value associated with the given key. If the key is not found in %config of the calling package, the next upper package is searched, following @ISA.
This works quite OK, but it is sort of very inconvient to change values in arrays or hashes. If I want to override a part of list of some sort, I have to put the new complete list in the Child-Config.
Eg:
in parent.cfg
#...
list=>['val1','val2','val3'],
in child.cfg
#...
list=>['val1','val2','val3','FOO','BAR'],
This results in a lot of very similar config entries.
What I want:
I would like to do something like that:
parent.cfg:
%config=(
# complex data structur
)
child.cfg:
config_add('newval',$datastruct);
config_remove('DontNeedThisHere');
config_push('array',$newval,$anotherval);
# and some other data manipulation routines
the changes in child.cfg should only be visible in Parent::Child ,e.g
if you do
print scalar (@{Parent->config('array')})
it should print "26", but
print scalar (@{Parent::Child->config('array')})
should print "28".
Finally, my questions:
- Before I implement it own my one: Is there a module on CPAN that implements some of this functionality? I couldn't find one.
- If not, have you got some Namespace suggestions? I was think of something like Data::Inheritable or Data::OO
- Do you know of another place to ask this questions?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.