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:# %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 }
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.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 );
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)
In reply to Re: Yet Another Config File Parser Module
by deprecated
in thread Yet Another Config File Parser Module
by jynx
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |