line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package SysConf; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
61622
|
use 5.012; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
28
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
26
|
|
5
|
1
|
|
|
1
|
|
4
|
use warnings FATAL => 'all'; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
48
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
4
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
47
|
|
8
|
1
|
|
|
1
|
|
848
|
use Switch; |
|
1
|
|
|
|
|
27310
|
|
|
1
|
|
|
|
|
6
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
91283
|
use constant true => (1==1); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
74
|
|
11
|
1
|
|
|
1
|
|
6
|
use constant false => (1==0); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
560
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
my (%rc,%_mount); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 NAME |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
SysConf - Create/Read/Update files in CentOS and Red Hat sysconfig directory |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 VERSION |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
Version 0.14 |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=cut |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
our $VERSION = '0.14'; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head1 SYNOPSIS |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
use SysConf; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
my $sysconf_file = 'name_of_file'; |
36
|
|
|
|
|
|
|
my $sysconf_path = '/etc/sysconfig'; |
37
|
|
|
|
|
|
|
my $foo = SysConf->new({'file' => $sysconf_file ,'path' => $sysconf_path}); |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# attach the object to the file |
40
|
|
|
|
|
|
|
$foo->attach; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# get a list of all keys in the file (ignore commented ones) |
43
|
|
|
|
|
|
|
my @k = $foo->keys; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# set a particular key to a particular value (will insert the key if needed) |
46
|
|
|
|
|
|
|
$foo->update('bar'=>1); |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# get a particular value given a key |
49
|
|
|
|
|
|
|
my $val = $foo->retrieve('oof'); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# delete a key/value pair |
52
|
|
|
|
|
|
|
my $rv = $foo->delete('bar'); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# detach the object from the file |
55
|
|
|
|
|
|
|
$foo->detach; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
... |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=head1 SUBROUTINES/METHODS |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=cut |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub new { |
65
|
2
|
|
|
2
|
1
|
3133
|
my $this = shift; |
66
|
2
|
|
|
|
|
5
|
my $args = shift; |
67
|
2
|
|
33
|
|
|
45
|
my $class = ref($this) || $this; |
68
|
2
|
|
|
|
|
6
|
my $self = {}; |
69
|
2
|
|
|
|
|
5
|
bless $self, $class; |
70
|
2
|
100
|
|
|
|
9
|
if ($args) { |
71
|
1
|
|
|
|
|
2
|
foreach my $arg (keys %{$args}) { |
|
1
|
|
|
|
|
11
|
|
72
|
2
|
|
|
|
|
3
|
switch ($arg) { |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
10
|
|
|
0
|
|
|
|
|
0
|
|
73
|
2
|
100
|
|
|
|
37
|
case "path" { $self->path($args->{$arg})} |
|
1
|
|
|
|
|
20
|
|
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
11
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
74
|
1
|
50
|
|
|
|
17
|
case "file" { $self->file($args->{$arg})} |
|
1
|
|
|
|
|
15
|
|
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
8
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
75
|
0
|
0
|
|
|
|
0
|
case "debug" { $self->debug($args->{$arg})} |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
76
|
0
|
|
|
|
|
0
|
else { $self->{'_'.$arg}= $args->{$arg}} |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
} |
80
|
2
|
|
|
|
|
9
|
return $self; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=head2 |
84
|
|
|
|
|
|
|
path set or get the path in the file system where the file resides |
85
|
|
|
|
|
|
|
=cut |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub path { |
88
|
15
|
|
|
15
|
1
|
344
|
my $self = shift; |
89
|
15
|
100
|
|
|
|
36
|
if(@_) { $self->{path} = $_[0]; } |
|
2
|
|
|
|
|
5
|
|
90
|
15
|
|
|
|
|
53
|
return $self->{path}; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head2 file |
94
|
|
|
|
|
|
|
file set or get the name of the file |
95
|
|
|
|
|
|
|
=cut |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub file { |
98
|
14
|
|
|
14
|
1
|
388
|
my $self = shift; |
99
|
14
|
100
|
|
|
|
41
|
if(@_) { $self->{file} = $_[0]; } |
|
2
|
|
|
|
|
4
|
|
100
|
14
|
|
|
|
|
219
|
return $self->{file}; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head2 debug |
104
|
|
|
|
|
|
|
debug set or get the debugging switch |
105
|
|
|
|
|
|
|
=cut |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub debug { |
108
|
11
|
|
|
11
|
1
|
746
|
my $self = shift; |
109
|
11
|
100
|
|
|
|
29
|
if(@_) { $self->{debug} = $_[0]; } |
|
2
|
|
|
|
|
10
|
|
110
|
11
|
|
|
|
|
149
|
return $self->{debug}; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head2 new |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
new Create an instance of this object. You may |
117
|
|
|
|
|
|
|
initialize class variables with an anonymous hash |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=cut |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub keys { |
122
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
123
|
0
|
|
|
|
|
0
|
return keys %{$self->{'_conf'}}; |
|
0
|
|
|
|
|
0
|
|
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head2 keys |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
keys return a list of keys stored in the file |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=cut |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
sub attach { |
133
|
1
|
|
|
1
|
|
7
|
use File::Spec; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
281
|
|
134
|
3
|
|
|
3
|
1
|
7
|
my $self = shift; |
135
|
3
|
|
|
|
|
5
|
my %rc; |
136
|
3
|
|
|
|
|
8
|
my $full_path = File::Spec->catfile($self->path,$self->file); |
137
|
|
|
|
|
|
|
|
138
|
3
|
100
|
|
|
|
123
|
if (!-e $full_path) { |
139
|
1
|
50
|
|
|
|
4
|
printf STDERR "D[%i] touching file = %s\n",$$,$full_path if ($self->debug); |
140
|
1
|
50
|
|
|
|
102
|
open(my $fh, ">".$full_path) or die "FATAL ERROR: unable to open file = $full_path\n"; |
141
|
1
|
|
|
|
|
15
|
printf $fh '# this file intentionally left blank'."\n"; |
142
|
1
|
|
|
|
|
58
|
close($fh); |
143
|
|
|
|
|
|
|
} |
144
|
3
|
50
|
|
|
|
57
|
die "File $full_path not found" if (!-e $full_path); |
145
|
3
|
50
|
|
|
|
62
|
die "File $full_path not readable" if (!-r $full_path); |
146
|
3
|
50
|
|
|
|
60
|
carp "File $full_path not writable" if (!-w $full_path); |
147
|
3
|
|
|
|
|
12
|
$self->_read; |
148
|
3
|
|
|
|
|
9
|
$self->{'attached'}=true; |
149
|
3
|
|
|
|
|
18
|
return true; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
sub _read { |
153
|
1
|
|
|
1
|
|
7
|
use Data::Dumper; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
470
|
|
154
|
3
|
|
|
3
|
|
5
|
my $self = shift; |
155
|
3
|
|
|
|
|
9
|
my $file = File::Spec->catfile($self->path,$self->file); |
156
|
3
|
50
|
|
|
|
170
|
open(my $fh, "<".$file) or die "FATAL ERROR: unable to open file = $file\n"; |
157
|
3
|
|
|
|
|
6
|
my (@lines,$line,$count); |
158
|
3
|
|
|
|
|
6
|
$count=0; |
159
|
3
|
|
|
|
|
45
|
while ($line = <$fh>) { |
160
|
7
|
100
|
|
|
|
60
|
if ($line =~ /^\s{0,}(\S+)\s{0,}=\s{0,}(.*?)\s\#{0,}.*/) { |
161
|
6
|
|
|
|
|
26
|
$self->{'_conf'}->{$1} = $2; |
162
|
6
|
|
|
|
|
45
|
$count++; |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
|
166
|
3
|
|
|
|
|
34
|
close($fh); |
167
|
3
|
|
|
|
|
12
|
return $count; |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head2 attach |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=cut |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
sub detach { |
176
|
2
|
|
|
2
|
1
|
5
|
my $self = shift; |
177
|
2
|
|
|
|
|
6
|
my $file = File::Spec->catfile($self->path,$self->file); |
178
|
2
|
|
|
|
|
26
|
$self->_write; |
179
|
2
|
|
|
|
|
8
|
delete $self->{'_conf'}; |
180
|
2
|
|
|
|
|
6
|
$self->{'attached'}=false; |
181
|
2
|
|
|
|
|
10
|
return true; |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
sub _write { |
185
|
4
|
|
|
4
|
|
5
|
my $self = shift; |
186
|
4
|
|
|
|
|
11
|
my $file = File::Spec->catfile($self->path,$self->file); |
187
|
4
|
50
|
|
|
|
428
|
open(my $fh, ">".$file) or die "FATAL ERROR: unable to open file = $file\n"; |
188
|
4
|
|
|
|
|
9
|
while (my ($k,$v) = each %{$self->{'_conf'}}) { |
|
16
|
|
|
|
|
61
|
|
189
|
12
|
|
|
|
|
80
|
printf $fh "%s=%s\n",$k,$v; |
190
|
|
|
|
|
|
|
} |
191
|
4
|
|
|
|
|
153
|
close($fh); |
192
|
|
|
|
|
|
|
} |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=head2 detach |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=cut |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
sub retrieve { |
199
|
1
|
|
|
1
|
|
8
|
use Data::Dumper; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
391
|
|
200
|
4
|
|
|
4
|
1
|
6
|
my $self = shift; |
201
|
4
|
|
|
|
|
8
|
my $k = shift; |
202
|
4
|
50
|
|
|
|
11
|
return undef if (!$self->{'attached'}); |
203
|
|
|
|
|
|
|
|
204
|
4
|
50
|
|
|
|
11
|
printf STDERR "D[%i] key = %s\n",$$,$k if ($self->debug); |
205
|
|
|
|
|
|
|
|
206
|
4
|
50
|
|
|
|
15
|
if (defined($self->{'_conf'})) |
207
|
|
|
|
|
|
|
{ |
208
|
4
|
|
|
|
|
5
|
my %h = %{$self->{'_conf'}}; |
|
4
|
|
|
|
|
22
|
|
209
|
4
|
|
|
|
|
7
|
my $v; |
210
|
4
|
100
|
|
|
|
22
|
$v = $h{$k} if (defined($h{$k})); |
211
|
4
|
100
|
|
|
|
9
|
printf STDERR "D[%i] val = %s\n",$$,( defined($v) ? $v : "undef") if ($self->debug); |
|
|
50
|
|
|
|
|
|
212
|
4
|
|
|
|
|
26
|
return $v; |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
else |
215
|
0
|
|
|
|
|
0
|
{ return undef } |
216
|
|
|
|
|
|
|
} |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=head2 retrieve |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=cut |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
sub update { |
224
|
2
|
|
|
2
|
1
|
5
|
my $self = shift; |
225
|
2
|
|
|
|
|
2
|
my $kvp = shift; |
226
|
2
|
50
|
|
|
|
9
|
return undef if (!$self->{'attached'}); |
227
|
2
|
|
|
|
|
3
|
my ($k,$v); |
228
|
2
|
|
|
|
|
4
|
while (($k,$v) = each %{$kvp}) { $self->{'_conf'}->{$k} = $v; } |
|
8
|
|
|
|
|
30
|
|
|
6
|
|
|
|
|
17
|
|
229
|
2
|
|
|
|
|
8
|
$self->_write; |
230
|
|
|
|
|
|
|
} |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=head2 update |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=cut |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
sub delete { |
237
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
238
|
0
|
|
|
|
|
|
my $k = shift; |
239
|
0
|
0
|
|
|
|
|
if (defined($self->{'conf'})) |
240
|
|
|
|
|
|
|
{ |
241
|
0
|
0
|
|
|
|
|
if (defined($self->{'_conf'}->{$k})) |
242
|
|
|
|
|
|
|
{ |
243
|
0
|
|
|
|
|
|
delete $self->{'_conf'}->{$k}; |
244
|
0
|
|
|
|
|
|
return true; |
245
|
|
|
|
|
|
|
} |
246
|
|
|
|
|
|
|
else |
247
|
0
|
|
|
|
|
|
{ return false ;} |
248
|
|
|
|
|
|
|
} |
249
|
|
|
|
|
|
|
else |
250
|
0
|
|
|
|
|
|
{ return undef } |
251
|
|
|
|
|
|
|
} |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
=head2 delete |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
=cut |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
=head1 AUTHOR |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
Joe Landman, C<< >> |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
=head1 BUGS |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
265
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
266
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
=head1 SUPPORT |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
perldoc SysConf |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
You can also look for information at: |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
=over 4 |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker (report bugs here) |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
L |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
L |
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
=item * CPAN Ratings |
291
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
L |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
=item * Search CPAN |
295
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
L |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
=back |
299
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
Copyright 2013 Scalable Informatics. |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify |
309
|
|
|
|
|
|
|
it under the terms of the GNU General Public License as published by |
310
|
|
|
|
|
|
|
the Free Software Foundation; version 2 dated June, 1991 or at your option |
311
|
|
|
|
|
|
|
any later version. |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, |
314
|
|
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
315
|
|
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
316
|
|
|
|
|
|
|
GNU General Public License for more details. |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
A copy of the GNU General Public License is available in the source tree; |
319
|
|
|
|
|
|
|
if not, write to the Free Software Foundation, Inc., |
320
|
|
|
|
|
|
|
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
=cut |
324
|
|
|
|
|
|
|
|
325
|
|
|
|
|
|
|
1; # End of SysConf |