-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfigUtil.pm
51 lines (45 loc) · 1.02 KB
/
ConfigUtil.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package ConfigUtil;
=start
ConfigUtil读取配置文件工具类
my $config = new ConfigUtil(DB,'< db.properties',$log);
my $value = $config->get('db.database');
say $value;
=cut
use 5.010;
sub new{
my($pack,$filehandler,$filename,$log) = @_;
my $fh = <$filehandler>;
my %hash;
open($fh,$filename) or $log->info("Can't open config file \"$filename\" : $!");
$log->info("开始读取配置文件 \"$filename\"");
while(<$fh>) {
chomp;
next if /^#|^$/;
my ($k,$v) = split/\s*\=\s*/;
chomp( $k );
chomp( $v );
$hash{$k} = $v;
}
$log->info("读取配置文件结束 \"$filename\"");
close($fh) or $log->info("Can't close config file \"$filename\" : $!");
my $self = {
filehandler => $fh,
hash => \%hash,
log => $log,
};
return bless $self,$pack;
}
sub get{
my ($self,$key) = @_;
my $value = $self->{hash}{$key};
return $value;
}
sub set{
my ($self,$key,$value) = @_;
$self->{hash}{$key} = $value;
}
sub getAll{
my $self = shift;
return $self->{hash};
}
1;