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: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>
and the perl code to read that# 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
the Alt:whatever:vars thing is there becuase you can take the third part of that and make it into another hash..#!/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"; }
In reply to Re: Configuration file parsing?
by cleen
in thread Configuration file parsing?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |