| 1 | package Bot::Infobot::Config; | ||||||||||||
| 2 | |||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use vars qw(@EXPORT_OK); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 1 | ▶ | use Exporter; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | ▶ | use base qw(Exporter); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 1 | ▶ | use Config::Tiny; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | |||||||||||||
| 11 | 1 | @EXPORT_OK = qw(parse_config save_config); | |||||||||||
| 12 | |||||||||||||
| 13 | =head1 NAME | ||||||||||||
| 14 | |||||||||||||
| 15 | Bot::Infobot::Config - parse Bot::Infobot config files | ||||||||||||
| 16 | |||||||||||||
| 17 | =head1 SYNOPSIS | ||||||||||||
| 18 | |||||||||||||
| 19 | use Bot::Infobot::Config qw(parse_config); | ||||||||||||
| 20 | |||||||||||||
| 21 | my %config = parse_config('infobot.conf'); | ||||||||||||
| 22 | |||||||||||||
| 23 | |||||||||||||
| 24 | =head1 METHODS | ||||||||||||
| 25 | |||||||||||||
| 26 | =head2 parse_config <config file> | ||||||||||||
| 27 | |||||||||||||
| 28 | Returns a hash of config values. | ||||||||||||
| 29 | |||||||||||||
| 30 | Sub parts are in sub hashes. For example | ||||||||||||
| 31 | |||||||||||||
| 32 | foo = bar; | ||||||||||||
| 33 | |||||||||||||
| 34 | [ Sub ] | ||||||||||||
| 35 | quirka = fleeg | ||||||||||||
| 36 | |||||||||||||
| 37 | would be converted to | ||||||||||||
| 38 | |||||||||||||
| 39 | ( | ||||||||||||
| 40 | 'foo' => 'bar', | ||||||||||||
| 41 | 'sub' => { | ||||||||||||
| 42 | 'quirka' => 'fleeg', | ||||||||||||
| 43 | } | ||||||||||||
| 44 | ) | ||||||||||||
| 45 | |||||||||||||
| 46 | =cut | ||||||||||||
| 47 | |||||||||||||
| 48 | 0 | ▶ | sub parse_config { | ||||||||||
|
Subroutine
parse_config: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||
| 49 | 0 | ▶ | my $file = shift || die "You must pass a file"; | ||||||||||
|
Condition0%0/2
shift() || die('You must pass a file')
MC/DC0%0/1
shift() || die('You must pass a file')
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||
| 50 | |||||||||||||
| 51 | # read the config, if it exists | ||||||||||||
| 52 | 0 | my $config = Config::Tiny->read($file); | |||||||||||
| 53 | |||||||||||||
| 54 | # read all the root config values in, splitting where necessary | ||||||||||||
| 55 | 0 | my %conf; | |||||||||||
| 56 | 0 | foreach my $key (keys %{$config->{_}}) { | |||||||||||
| 57 | 0 | my $val = $config->{_}->{$key}; | |||||||||||
| 58 | 0 | $conf{$key} = $val; | |||||||||||
| 59 | } | ||||||||||||
| 60 | 0 | delete $config->{_}; | |||||||||||
| 61 | 0 | ▶ | while (my ($key,$val) = each %{$config}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 62 | 0 | $key =~ s!(^\s*|\s*$)!!g; | |||||||||||
| 63 | 0 | ▶ | if ($key eq 'Store') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 64 | 0 | $conf{'store'} = $val; | |||||||||||
| 65 | } else { | ||||||||||||
| 66 | 0 | $conf{$key} = $val; | |||||||||||
| 67 | } | ||||||||||||
| 68 | } | ||||||||||||
| 69 | |||||||||||||
| 70 | 0 | return %conf; | |||||||||||
| 71 | } | ||||||||||||
| 72 | |||||||||||||
| 73 | |||||||||||||
| 74 | =head2 save_config <file> <hash of values> | ||||||||||||
| 75 | |||||||||||||
| 76 | Save the config back out again. | ||||||||||||
| 77 | |||||||||||||
| 78 | =cut | ||||||||||||
| 79 | |||||||||||||
| 80 | 0 | ▶ | sub save_config { | ||||||||||
|
Subroutine
save_config: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||
| 81 | 0 | ▶ | my $file = shift || die "You must pass a file"; | ||||||||||
|
Condition0%0/2
shift() || die('You must pass a file')
MC/DC0%0/1
shift() || die('You must pass a file')
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||
| 82 | 0 | ▶ | my $conf = (-f $file)? Config::Tiny->read($file) : Config::Tiny->new(); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 83 | 0 | my %vars = @_; | |||||||||||
| 84 | |||||||||||||
| 85 | |||||||||||||
| 86 | 0 | ▶ | while (my($key,$val) = each %vars) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 87 | 0 | ▶ | $val = join " ", @$val if ref $val eq 'ARRAY'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 88 | 0 | ▶ | if (ref $val eq 'HASH') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 89 | 0 | ▶ | $key = 'Store' if $key eq 'store'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 90 | 0 | $conf->{" $key "} = $val; | |||||||||||
| 91 | 0 | next; | |||||||||||
| 92 | } | ||||||||||||
| 93 | 0 | $conf->{_}->{$key} = $val; | |||||||||||
| 94 | |||||||||||||
| 95 | } | ||||||||||||
| 96 | |||||||||||||
| 97 | 0 | $conf->write($file); | |||||||||||
| 98 | } | ||||||||||||
| 99 | |||||||||||||
| 100 | =head1 AUTHOR | ||||||||||||
| 101 | |||||||||||||
| 102 | Simon Wistow <simon@thegestalt.org> | ||||||||||||
| 103 | |||||||||||||
| 104 | =head1 COPYRIGHT | ||||||||||||
| 105 | |||||||||||||
| 106 | Copyright 2005, Simon Wistow | ||||||||||||
| 107 | |||||||||||||
| 108 | Distributed under the same terms as Perl itself. | ||||||||||||
| 109 | |||||||||||||
| 110 | =cut | ||||||||||||
| 111 | |||||||||||||
| 112 | 1 | 1; | |||||||||||
| 113 | |||||||||||||