| Bot-Infobot documentation | Contained in the Bot-Infobot distribution. |
Bot::Infobot::Config - parse Bot::Infobot config files
use Bot::Infobot::Config qw(parse_config);
my %config = parse_config('infobot.conf');
Returns a hash of config values.
Sub parts are in sub hashes. For example
foo = bar;
[ Sub ]
quirka = fleeg
would be converted to
(
'foo' => 'bar',
'sub' => {
'quirka' => 'fleeg',
}
)
Save the config back out again.
Simon Wistow <simon@thegestalt.org>
Copyright 2005, Simon Wistow
Distributed under the same terms as Perl itself.
| Bot-Infobot documentation | Contained in the Bot-Infobot distribution. |
package Bot::Infobot::Config; use strict; use vars qw(@EXPORT_OK); use Exporter; use base qw(Exporter); use Config::Tiny; @EXPORT_OK = qw(parse_config save_config);
sub parse_config { my $file = shift || die "You must pass a file"; # read the config, if it exists my $config = Config::Tiny->read($file); # read all the root config values in, splitting where necessary my %conf; foreach my $key (keys %{$config->{_}}) { my $val = $config->{_}->{$key}; $conf{$key} = $val; } delete $config->{_}; while (my ($key,$val) = each %{$config}) { $key =~ s!(^\s*|\s*$)!!g; if ($key eq 'Store') { $conf{'store'} = $val; } else { $conf{$key} = $val; } } return %conf; }
sub save_config { my $file = shift || die "You must pass a file"; my $conf = (-f $file)? Config::Tiny->read($file) : Config::Tiny->new(); my %vars = @_; while (my($key,$val) = each %vars) { $val = join " ", @$val if ref $val eq 'ARRAY'; if (ref $val eq 'HASH') { $key = 'Store' if $key eq 'store'; $conf->{" $key "} = $val; next; } $conf->{_}->{$key} = $val; } $conf->write($file); }
1;