| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package DB::SimpleKV; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
76116
|
use 5.006; |
|
|
1
|
|
|
|
|
3
|
|
|
4
|
1
|
|
|
1
|
|
4
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
45
|
|
|
5
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
786
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 NAME |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
DB::SimpleKV - Simple k/v interface to text configuration file |
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 VERSION |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
Version 0.01 |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=cut |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub new { |
|
20
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
|
21
|
0
|
|
0
|
|
|
|
my $file = shift || "/tmp/simplekv.db"; |
|
22
|
|
|
|
|
|
|
|
|
23
|
0
|
0
|
|
|
|
|
unless (-f $file) { |
|
24
|
0
|
0
|
|
|
|
|
open my $db,">",$file or die $!; |
|
25
|
0
|
|
|
|
|
|
close $db; |
|
26
|
|
|
|
|
|
|
} |
|
27
|
|
|
|
|
|
|
|
|
28
|
0
|
|
|
|
|
|
bless { file=>$file }, $class; |
|
29
|
|
|
|
|
|
|
} |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub exists { |
|
32
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
33
|
0
|
|
|
|
|
|
my $key = shift; |
|
34
|
0
|
|
|
|
|
|
my $found = 0; |
|
35
|
|
|
|
|
|
|
|
|
36
|
0
|
0
|
|
|
|
|
open my $db, $self->{file} or die $!; |
|
37
|
0
|
|
|
|
|
|
while(<$db>) { |
|
38
|
0
|
|
|
|
|
|
my ($k,$v) = split/=/; |
|
39
|
0
|
|
|
|
|
|
$k =~ s/^\s+|\s+$//g; |
|
40
|
0
|
|
|
|
|
|
$v =~ s/^\s+|\s+$//g; |
|
41
|
0
|
0
|
|
|
|
|
if ($k eq $key) { |
|
42
|
0
|
|
|
|
|
|
$found =1; |
|
43
|
0
|
|
|
|
|
|
last; |
|
44
|
|
|
|
|
|
|
} |
|
45
|
|
|
|
|
|
|
} |
|
46
|
0
|
|
|
|
|
|
close $db; |
|
47
|
|
|
|
|
|
|
|
|
48
|
0
|
|
|
|
|
|
return $found; |
|
49
|
|
|
|
|
|
|
} |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub set { |
|
52
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
53
|
0
|
|
|
|
|
|
my $key = shift; |
|
54
|
0
|
|
|
|
|
|
my $value = shift; |
|
55
|
0
|
|
|
|
|
|
my %hash; |
|
56
|
|
|
|
|
|
|
|
|
57
|
0
|
0
|
|
|
|
|
open my $db, $self->{file} or die $!; |
|
58
|
0
|
|
|
|
|
|
while(<$db>) { |
|
59
|
0
|
|
|
|
|
|
my ($k,$v) = split/=/; |
|
60
|
0
|
|
|
|
|
|
$k =~ s/^\s+|\s+$//g; |
|
61
|
0
|
|
|
|
|
|
$v =~ s/^\s+|\s+$//g; |
|
62
|
0
|
|
|
|
|
|
$hash{$k} = $v; |
|
63
|
|
|
|
|
|
|
} |
|
64
|
0
|
|
|
|
|
|
close $db; |
|
65
|
|
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
|
$hash{$key} = $value; |
|
67
|
|
|
|
|
|
|
|
|
68
|
0
|
0
|
|
|
|
|
open my $dbx, ">", $self->{file} or die $!; |
|
69
|
0
|
|
|
|
|
|
for (sort keys %hash) { |
|
70
|
0
|
|
|
|
|
|
print $dbx $_,"=",$hash{$_},"\n"; |
|
71
|
|
|
|
|
|
|
} |
|
72
|
0
|
|
|
|
|
|
close $dbx; |
|
73
|
|
|
|
|
|
|
} |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub delete { |
|
76
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
77
|
0
|
|
|
|
|
|
my $key = shift; |
|
78
|
0
|
|
|
|
|
|
my %hash; |
|
79
|
|
|
|
|
|
|
|
|
80
|
0
|
0
|
|
|
|
|
open my $db, $self->{file} or die $!; |
|
81
|
0
|
|
|
|
|
|
while(<$db>) { |
|
82
|
0
|
|
|
|
|
|
my ($k,$v) = split/=/; |
|
83
|
0
|
|
|
|
|
|
$k =~ s/^\s+|\s+$//g; |
|
84
|
0
|
|
|
|
|
|
$v =~ s/^\s+|\s+$//g; |
|
85
|
0
|
|
|
|
|
|
$hash{$k} = $v; |
|
86
|
|
|
|
|
|
|
} |
|
87
|
0
|
|
|
|
|
|
close $db; |
|
88
|
|
|
|
|
|
|
|
|
89
|
0
|
|
|
|
|
|
delete $hash{$key}; |
|
90
|
|
|
|
|
|
|
|
|
91
|
0
|
0
|
|
|
|
|
open my $dbx, ">", $self->{file} or die $!; |
|
92
|
0
|
|
|
|
|
|
for (sort keys %hash) { |
|
93
|
0
|
|
|
|
|
|
print $dbx $_,"=",$hash{$_},"\n"; |
|
94
|
|
|
|
|
|
|
} |
|
95
|
0
|
|
|
|
|
|
close $dbx; |
|
96
|
|
|
|
|
|
|
} |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub get { |
|
99
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
100
|
0
|
|
|
|
|
|
my $key = shift; |
|
101
|
0
|
|
|
|
|
|
my $value = undef; |
|
102
|
|
|
|
|
|
|
|
|
103
|
0
|
0
|
|
|
|
|
open my $db, $self->{file} or die $!; |
|
104
|
0
|
|
|
|
|
|
while(<$db>) { |
|
105
|
0
|
|
|
|
|
|
my ($k,$v) = split/=/; |
|
106
|
0
|
|
|
|
|
|
$k =~ s/^\s+|\s+$//g; |
|
107
|
0
|
|
|
|
|
|
$v =~ s/^\s+|\s+$//g; |
|
108
|
0
|
0
|
|
|
|
|
if ($k eq $key) { |
|
109
|
0
|
|
|
|
|
|
$value = $v; |
|
110
|
0
|
|
|
|
|
|
last; |
|
111
|
|
|
|
|
|
|
} |
|
112
|
|
|
|
|
|
|
} |
|
113
|
0
|
|
|
|
|
|
close $db; |
|
114
|
|
|
|
|
|
|
|
|
115
|
0
|
|
|
|
|
|
return $value; |
|
116
|
|
|
|
|
|
|
} |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
This module is mainly used to manipulate a configuration file like Postfix's main.cf |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
It creates the default db file under "/tmp/simplekv.db". |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
use DB::SimpleKV; |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
my $db = DB::SimpleKV->new; |
|
129
|
|
|
|
|
|
|
$db->set("hostname","h99.foo.com"); |
|
130
|
|
|
|
|
|
|
$db->set("provider","rackspace cloud"); |
|
131
|
|
|
|
|
|
|
$db->set("ip_addr","192.168.2.10"); |
|
132
|
|
|
|
|
|
|
$db->set("netmask","255.255.255.0"); |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
print $db->get("provider"),"\n"; |
|
135
|
|
|
|
|
|
|
$db->delete("netmask"); |
|
136
|
|
|
|
|
|
|
print "netmask exists? ", $db->exists("netmask") ? "yes" : "no", "\n"; |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Or you can specify the existing file for manipulation, one configuration per line, with '=' as delimiter. |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
use DB::SimpleKV; |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
my $db = DB::SimpleKV->new("/etc/postfix/main.cf"); |
|
143
|
|
|
|
|
|
|
print $db->get("relayhost"),"\n"; |
|
144
|
|
|
|
|
|
|
print "relay exists? ", $db->exists("relayhost") ? "yes" : "no", "\n"; |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head1 SUBROUTINES/METHODS |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head2 new |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
my $db = DB::SimpleKV->new(...); |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head2 get |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
my $value = $db->get("key"); |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=head2 set |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
$db->set("key","value"); |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head2 delete |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
$db->delete("key"); |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head2 exists |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
my $exists = $db->exists("key"); |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=head1 AUTHOR |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
Ken Peng, C<< >> |
|
174
|
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=head1 BUGS |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
|
178
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
|
179
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head1 SUPPORT |
|
185
|
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
|
187
|
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
perldoc DB::SimpleKV |
|
189
|
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
You can also look for information at: |
|
192
|
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=over 4 |
|
194
|
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker (report bugs here) |
|
196
|
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
L |
|
198
|
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=item * CPAN Ratings |
|
200
|
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
L |
|
202
|
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=item * Search CPAN |
|
204
|
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
L |
|
206
|
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=back |
|
208
|
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
|
211
|
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
|
214
|
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
This software is Copyright (c) 2022 by Ken Peng. |
|
216
|
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
This is free software, licensed under: |
|
218
|
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
The Artistic License 2.0 (GPL Compatible) |
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
=cut |
|
223
|
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
1; # End of DB::SimpleKV |