in reply to Yet Another Config File Parser Module

seems like an awful lot of work to me. i actually wrote one this morning (admittedly not a module, but i dont see it as being necessary!) thats pretty easy to use and fits comfortably on 5 lines. this is actually my first posting so i hope the code turns out okay.
# %settings = grok ( qq(filename), %settings ); sub grok { my $n = shift; open IN, $n; my @in = <IN>; close IN; my %input = shi +ft; my %hash_to_return; foreach my $setting (keys %input) { my ($p_name, $p) = /^([^\s=]+)\b(?:(?:\s*=\s*)|\s+)(.*)$/; if ($setting eq $p_name) { $hash_to_return{$p_name} = $p } else { next } } return %hash_to_return } # # grok, above, posted earlier, is broken. :) it was actually # an old version (old being > 12 hours in this case) # I had in the wrong directory. Sorry for any confusion. # unfortunately, this one isnt nearly as pretty. # sub grok2 { my $n = shift; if ($n) { open IN, $n or warn "BDU error" } else { warn "BDU error" ; return } @in = <IN>; close IN; my %input = shift; foreach my $item (@in) { my ($p_name, $p) = $item =~ /^([^\s=]+)\b(?:(?:\s*=\s*)|\s+)(.*)$/; foreach my $setting (keys %i +nput) { if ($setting eq $p_name) { $input{$setting} = $p } else { next } } + } return %hash_to_return } # note some checking for files/vars was added for chipmunk. # nyah. :p # de_grok ( qq(filename), %settings ); sub de_grok { my $n = shift; open OUT, $n; my %output = shift; foreach my $line (keys %output) { print OUT $line . ' ' . $output{ $line } } close OUT }
as i recall, this compiles okay with -w and strict turned on. config file looks like "config_item the rest of the line are the arguments to config_item." since some users are kinda dumb about config files i allowed for a "config_var = params" too. the way i use this is like so:
my %CONFIG = ( server_name => 'localhost', server_alias => 'opennap', server_ports => '4444 7777 8888', max_user_channels => '5', max_nick_length => '32', ); %CONFIG = grok ( qq(config), %CONFIG );
i use this code in several applications. all of these focus on my opennap server. i set up my administration client-bots to grok in their config files (several actually) and the server reads its configs when it starts up and writes them to the disk every so often in case it crashes. its extremely versatile in that you can use it for userdb's, channeldb's, mp3 playlists, and so on.

cheers,
deprecated
(server in question is located at macachu.yi.org if you wanna have a looksee at an opennap server being developed in perl)

Replies are listed 'Best First'.
Re: Re: Yet Another Config File Parser Module
by jynx (Priest) on Dec 21, 2000 at 00:42 UTC
    deprecated,

    true, that does fit in five lines. i have a general aversion to the copy-paste right-click however so rather than having to paste a function in over a dozen files, i'd rather call a module that has all the functionality instead.

    Have you checked out App::Config? It does exactly what your code does and more. The reason i didn't use it was a couple dozen configuration settings and i didn't want to set them all in every file before reading in the configuration. It's another aversion to redundancy...

    happy coding,
    jynx