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.

In reply to Re: Configuration file parsing? by cleen
in thread Configuration file parsing? by Anonymous Monk

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.