Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Reading and *writing* configuration files

by DeathBunny (Sexton)
on Apr 08, 2002 at 22:49 UTC ( #157596=perlquestion: print w/replies, xml ) Need Help??
DeathBunny has asked for the wisdom of the Perl Monks concerning the following question:

I'm looking for suggestions/methods/idioms for reading and editing configuration files.

Specifically, I've got a CGI application that read and writes Bind 8.x/9.x named.conf files. I've got a parser mostly written for reading the named.conf file. I can write out a new named.conf file just fine *if* I don't mind blowing away any comments or hand formatting of the file.

I've occasionally seen programs that will read and edit configuration files while making at least some effort to preserve comments and hand edits of the file. I'd love to be able to do that, but I'm pretty much at a loss for where to begin.

Do any monks have any suggestions? Any Perl modules or scripts out there that are good examples of doing this kind of thing?

  • Comment on Reading and *writing* configuration files

Replies are listed 'Best First'.
Re: Reading and *writing* configuration files
by tachyon (Chancellor) on Apr 08, 2002 at 23:48 UTC

    There is BIND::Conf_Parser which is a base class for parsing 8.x named.conf files. In a nutshell to retain bits of an existing conf file you first need to parse it so you can find the bits you want to keep, change the bits you want to change, then put Humpty Dumpty back together again....




      I've looked at Bind::Conf, but it only reads named.conf files, it doesn't write them. :-(

        Ah that's kinda the idea. You read in the current file and parse it using a parser module. *You* make your changes to this parsed data set. *You* print out the new file from your munged data set.

        One significant problem with retaining comments is that they are only useful if the are relevant. Comments that relate to parts of the file which have been changed are worse than useless.




Re: Reading and *writing* configuration files
by Fletch (Chancellor) on Apr 09, 2002 at 02:08 UTC

    Another option would be to use something like Template Toolkit to process a skelleton file. If the user has any non-standard edits, they can place them in the skelleton file and they'll be preserved in the output. That of course requires some handwaving if the user expects something of theirs to override the generated output (perhaps use some sort of directive which sets a variable that the generated portion inspects).

      Text::Template might be worth looking at, too. I use that one for Java Servlet configs.
      One of these days, I'll get around to playing with Template Toolkit... I continue to hear much good about it.
      Also worth looking into, for its simplicity if for no other reason, is CGI::FastTemplate. The "CGI" in its name can be misleading -- it's just a darn fine template parser. I use it all the time, and often for things that get nowhere near the web.
Re: Reading and *writing* configuration files
by seanbo (Chaplain) on Apr 09, 2002 at 12:52 UTC
    You may want to take a look at Perl for System Administration by O'Reilly and Associates. Chapter 5 (TCP/IP Name Services) pages 149-167 describe generating BIND config files from a db. You can make all your changes to the db (through CGI if you like) and then dump the config files from the db. This way, there is no need to parse the existing files because the data is stored in the db.

    I highly recommend taking a look at this chapter. Even though the example is rather simplistic and wouldn't suit the needs where I work, it could be easily modified to incorporate the changes we need. We are looking into moving in this direction in the future.

    perl -e 'print reverse qw/o b n a e s/;'
Re: Reading and *writing* configuration files
by gav^ (Curate) on Apr 09, 2002 at 03:51 UTC
    I don't know anything about the file format, but I'm assuming that you take the file and have it in some kind of parsed state (LoH perhaps?). Have you considered using something like Algorithm::Diff to compare your changed file to the original and only outputting lines that have changed.

    Config::IniFiles tries to preserve comments but I'm not sure how well this works.

    Hope this helps...


      Hmmm.. those are not bad ideas. I don't know why I didn't think of Algorithm::Diff. I'll look at Config::IniFiles first to see if I can steal any ideas, then checkout Algorithm::Diff and see if it can be of any help.


Re: Reading and *writing* configuration files
by CukiMnstr (Deacon) on Apr 09, 2002 at 02:53 UTC
    Another option would be to have the 'default' (or changed trough your CGI) values in some sort of database (could be CSV). Then when you are generating the configuration from the CGI, you could merge both configs and keep the hand edits (by comparing existing tokens) and comments from the configuration file (comments are *somewhat* easier, since they probably have an 'opening token').

    yet another option would be to put and 'End of CGI config' tag at the end (or beginning) of the cgi-generated part, and then add some comments instructing the user making the hand edits to put his or her config directives in the proper section...

    hope this helps,

Re: Reading and *writing* configuration files
by Stegalex (Chaplain) on Apr 09, 2002 at 12:48 UTC
    Here's some HERESY for you: You can use Config::IniFiles if you want to use Window$-like .ini files. Works well. Check it:
    use Config::IniFiles; my $handle = Config::IniFiles->new(-file=>$filename); my $setting = $handle->val ('sectionname','yoursetting') print "I like chicken.\n";
Re: Reading and *writing* configuration files
by DaWolf (Curate) on Apr 09, 2002 at 02:28 UTC
    Hi there.

    I don't know almost anything about conf files, but I tought I could add one generic comment that MAYBE will help you.

    Sorry if it doesn't.

    Is there a pattern for a comment on them? Probably, right? So you could find a way to write a "simple" (the quotes are here because I can't tell the real complexity of your task) sub that ensures that anything matching that pattern would be preserved.

    I know this sound pretty obvious, but sometimes we forgot the simple things - happens a lot to me.

    Hope It helps you.

    Best regards,

    Er Galv„o Abbott
    a.k.a. Lobo, DaWolf
Re: Reading and *writing* configuration files
by sporte01 (Acolyte) on Apr 09, 2002 at 16:55 UTC
    Here's an idea. WHy not write your own format in XML, do all your management in XML, and use XSLT to write bind format files. That way if the BIND people decide, "wait, let's use format X", you have less to worry about. Just rewrite the XSLT and you won't have to change your programming. well.. just that inside the xslt, but that's easy enough :) At least you won't have to write another script to go from bind 9.x -> bind 9+n.x
      Dear god I hope your kidding! If not, I hope you'll elaborate. That sounds complicated and roundabout as hell, but that could just be because of my lack of XML knowledge.
Webmin might be a good example
by SwellJoe (Scribe) on Apr 09, 2002 at 19:23 UTC
    For what it's worth, Webmin has a nice BIND module that reads and writes BIND configuration files (all of them), and it respects comments and the existing formatting.

    The code isn't as modular as one might like, but it is thoroughly readable in all of the modules I've studied. It is probably worth looking into as a working example.

    It can be found here:

Re: Reading and *writing* configuration files
by archimago (Pilgrim) on Apr 09, 2002 at 20:34 UTC
    Interesting article on IBM developer works covers this ground and recommends the AppConfig module:

      While AppConfig is cool (used it myself several times), it doesn't read Bind config files and I don't think that it can output configurations (which was one of the points in the original question).

        Outputting configs was the ENTIRE point of the original article. I'm already parsing the config just fine. And, as you said, Appconfig while awesome, won't read named.conf files.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://157596]
Approved by premchai21
Front-paged by IlyaM
[Discipulus]: ...mmh Padre installed but fails to start
[marto]: Yeah, not a good start for a 'getting started' routine :)
[Discipulus]: at glance no unicode support by padre

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2018-06-25 09:15 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.