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

hi,
I will be generating a bunch of different config files in my case dhcpd.conf, tcng.conf (traffic control), docsis, probably dns zones, address user base and so on...
I can do it one by one but this soon will get complicated, on the other hand i'm pressed from the time (so can't think and implement do-it-all solution, but mostly something that can be extended easly).. I will explain my current thoughts, and hope u will give me better ones :"). Or probably will find my ideas good enought :")
All files at the moment will be generated from hashes or pipe-delimed text files. I have to leave some "door-open" for upgading this to SQL DB..
So on the question..
The main problem with the generation comes 'cause the generation-algorithm may benefit if not doing generation in linear fashion i.e. from top to down (begin to end of file).
Then comes the next decision I have to split the file on blocks and fire events on every block i.e. via callbacks ? The next consequence from this is that I have to build config-file skeleton (which I want to miss very much :"), probably not possible)
To get better picture here is one simple skeleton :
#include header.dhcp group { #block subnet1 subnet1Sub } #block hosts-declaration hostdeclSub #include footer.dhcp

Then in the perl program pseudo code :
read&parse(skeleton); run(); sub hostdeclSub { $res .= "host $hosts{$_}{name} { hardware-address $hosts{$_}{mac +} }\n" for keys %hosts return $res }

This is very simplified example.. there should be pre,post block callbacks, some identetion system should be included.. Probably "block" hierarcy too (tree-like may be).. Plus the ability of one block to call-inqury another block generation with ability to stop infinte recursion, etc,etc...
Some status info should be available in callbacks (such as in which block i'm?, what has been generated in the prev block?, does the block X have string 'blah'?...)
switch on-off generation of some blocks in perl and/or in skeleton..
this start to seem like macro system with callbacks in Perl :"))
Your ideas... what comes to your mind when u want to generate config files..
hmm........ can "Inline"-module help in some way !? :")

Replies are listed 'Best First'.
Re: config file generation
by data64 (Chaplain) on Feb 21, 2003 at 11:02 UTC
      hmm.. when i think more it seems Template::Toolkit to be the best solution... and i have used it alot for web programing... thanx :")
      I will need something lighter and easy to install... now looking at : http://search.cpan.org/author/SUMMER/Text-MacroScript-1.37/MacroScript.pm
Re: config file generation
by Anonymous Monk on Feb 21, 2003 at 20:43 UTC
    I don't fully understand your problem, you lost me at some point. But to give you something to chew on. I would assume that your config data could be stored in some form of hash. Design a sample hash that holds the data you think you will want to process. Thne use XML::simple to convert it into an XML file. Now you have an XML file designed specificly for your data and you can use XML::simple and one line of code to repopulate your hash. Something as simple as the following could generate your XML config file.
    use XML::simple; my %data = { Ball=>blue,sky=>clear }; my $hashref=/%data; my $xml = XMLout($hashref []);