in reply to Configuration file parsing?

personally, I think a better question is not "whats the best way to parse config files" but "How can I make my config file better for parsing?". Yes it might be taking a longer route (as in thought process wise) but can be truly helpful in the future.

The way I always go about thinking about making a config file is saying to myself "how can I get this all in a hash?" hehe yeah it might not be good to think that way, but its how _I_ think.

When I want to make an easily understood, easily extendable, strong config file, I make use of label's, static information, and alternatives. Ok so that doesnt make much sense...Lets put it this way:

Label:<variable-label> Static-Info-1:<variable-label>: <some-info-1> Static-Info-2:<variable-label>: <some-info-2> Alternative:<variable-label>: Static-alt-Info-1 <static-alt-info-1> Alternative:<variable-label>: Static-alt-Info-2 <static-alt-info-2>
So basically label becomes my holder for each static and alternative setting in my config file, so I can refer back to it at a future point in time with my program. In essence even the Label: part is a static peice of information.. To show how this teknique can acctually minimize parsing to almost nothing, I will give a small example:
# Sample Config File Label:perl-monks:perl-monks Hostname:perl-monks:www.perlmonks.org Description:perl-monks:The Perl Monks Webpage Alt:perl-monks:MonkVar-1 SomeSubVar-1 SomeVar-1-value Alt:perl-monks:MonkVar-2 SomeSubVar-2 SomeVar-2-value Label:ackers:ackers Hostname:ackers:www.ackers.net Description:ackers:Just my Page Alt:ackers:AckVar-1 SomeSubVar-1 SomeVar-1-value Alt:ackers:AckVar-2 SomeSubVar-2 SomeVar-2-value
and the perl code to read that
#!/usr/bin/perl # Here I will define all the static parts of the config file in an arr +ay # this is a nessesity, not only for easily adding features to your con +fig # but to keep code changes down to a minimum :) my @carray = ("Label", "Hostname", "Description"); # pretend config.conf holds the information above hehe. open (CONF, "config.conf"); while (<CONF>) { s/\s+$//g; # remove whitespaces s/\s/ /g; # replace whitespace by space next if /^\s*\#/; # ignore comment lines s/\s*\#$//g; # ignore trailing comments next if /^\s*$/; # ignore empty lines ($one,$two,$three) = split(/:/); if ($one eq "Alt") { $alt = $one; $label = $two; $var = $three; foreach($three) { $$alt{$var} = "$label"; } } else { foreach ($one) { $$one{$two} = "$three"; } } } foreach $thing (keys %Label) { foreach $heh (@carray) { while ( ($k,$v) = each %$heh ) { print "Static Setting: $heh: $v\n" if ($k eq $thing); } } while ( ($k,$v) = each %Alt ) { print "Alternate Setting: $k\n" if ($v eq $thing); } print "\n"; }
the Alt:whatever:vars thing is there becuase you can take the third part of that and make it into another hash..

but I probably sound pretty stupid all the way through this...I guess Im just trying to give examples of easy extensibility in configuration.

Replies are listed 'Best First'.
RE: Re: Configuration file parsing?
by davorg (Chancellor) on Jun 22, 2000 at 13:44 UTC

    You're making heavy use of symbolic references in this code which means that it won't work under use strict. Symbolic references are usually best avoided for reasons discussed in perldoc perlref. A better solution might be to store all of the configuration information in a global hash.

    Also there are a couple of places where you loop over a scalar value, e.g.

    foreach ($one) { $$one{$two} = "$three"; }

    which seems a bit of a strange thing to do!


    --
    <http://www.dave.org.uk>

    European Perl Conference - Sept 22/24 2000
    <http://www.yapc.org/Europe/>