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:


In reply to OO Data Structures ? by domm

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



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.