blib/lib/SysConf.pm

filter: Statementstmt 81.7% 121 / 148 Branchbran 66.0% 37 / 56 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 90.9% 20 / 22 Podpod 100% 10 / 10 totaltotal 78.4% 189 / 241 CC 31 SCAR 16.6
CC
31
Cov
77%
delete
12.0
new
11.0
retrieve
7.3
CRAP 5.3
↑ Summary
1 package SysConf;
2  
3 2 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Switch;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use constant true => (1==1);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use constant false => (1==0);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 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 2 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 2 ▶sub new {
Subroutine new: called CC=7 SCAR=24.0
Pod documented
65 2 my $this = shift;
66 2 my $args = shift;
67 2 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
68 2 my $self = {};
69 2 bless $self, $class;
70 2 ▶ if ($args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args) 1 1 2
71 1 foreach my $arg (keys %{$args}) {
72 2 switch ($arg) {
73 0 ▶ case "path" { $self->path($args->{$arg})}
Branch: 1 branch
BranchTrueFalseTotal
if (&Switch::case("path")) 1 1 2
74 0 ▶ case "file" { $self->file($args->{$arg})}
Branch: 1 branch
BranchTrueFalseTotal
if (&Switch::case("file")) 1 0 1
75 0 ▶ case "debug" { $self->debug($args->{$arg})}
Branch: 1 branch
BranchTrueFalseTotal
if (&Switch::case('debug')) { } 0 0 0
76 0 else { $self->{'_'.$arg}= $args->{$arg}}
77 0 }
78 }
79 }
80 2 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 15 ▶sub path {
Subroutine path: called CC=2 SCAR=6.9
Pod documented
88 15 my $self = shift;
89 15 ▶ if(@_) { $self->{path} = $_[0]; }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 13 15
90 15 return $self->{path};
91 }
92  
93 =head2 file
94 file set or get the name of the file
95 =cut
96  
97 14 ▶sub file {
Subroutine file: called CC=2 SCAR=6.9
Pod documented
98 14 my $self = shift;
99 14 ▶ if(@_) { $self->{file} = $_[0]; }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 12 14
100 14 return $self->{file};
101 }
102  
103 =head2 debug
104 debug set or get the debugging switch
105 =cut
106  
107 11 ▶sub debug {
Subroutine debug: called CC=2 SCAR=6.9
Pod documented
108 11 my $self = shift;
109 11 ▶ if(@_) { $self->{debug} = $_[0]; }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 9 11
110 11 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 0 ▶sub keys {
Subroutine keys: not called CC=1 SCAR=6.9
Pod documented
122 0 my $self = shift;
123 0 return keys %{$self->{'_conf'}};
124 }
125  
126 =head2 keys
127  
128 keys return a list of keys stored in the file
129  
130 =cut
131  
132 3 ▶sub attach {
Subroutine attach: called CC=7 SCAR=19.8
Pod documented
133 2 ▶ use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
134 3 my $self = shift;
135 3 my %rc;
136 3 my $full_path = File::Spec->catfile($self->path,$self->file);
137
138 3 ▶ if (!-e $full_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $full_path) 1 2 3
139 1 ▶ printf STDERR "D[%i] touching file = %s\n",$$,$full_path if ($self->debug);
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 1 0 1
140 1 ▶ open(my $fh, ">".$full_path) or die "FATAL ERROR: unable to open file = $full_path\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, '>' . $full_path or die "FATAL ERROR: unable to open file = $full_path\n" 1 0 1
141 1 printf $fh '# this file intentionally left blank'."\n";
142 1 close($fh);
143 }
144 3 ▶ die "File $full_path not found" if (!-e $full_path);
Branch: 1 branch
BranchTrueFalseTotal
unless -e $full_path 0 3 3
145 3 ▶ die "File $full_path not readable" if (!-r $full_path);
Branch: 1 branch
BranchTrueFalseTotal
unless -r $full_path 0 3 3
146 3 ▶ carp "File $full_path not writable" if (!-w $full_path);
Branch: 1 branch
BranchTrueFalseTotal
unless -w $full_path 0 3 3
147 3 $self->_read;
148 3 $self->{'attached'}=true;
149 3 return true;
150 }
151  
152 3 ▶sub _read {
Subroutine _read: called CC=4 SCAR=13.9
153 2 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
154 3 my $self = shift;
155 3 my $file = File::Spec->catfile($self->path,$self->file);
156 3 ▶ open(my $fh, "<".$file) or die "FATAL ERROR: unable to open file = $file\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, '<' . $file or die "FATAL ERROR: unable to open file = $file\n" 3 0 3
157 3 my (@lines,$line,$count);
158 3 $count=0;
159 3 ▶ while ($line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline $fh)) 7 3 10
160 7 ▶ if ($line =~ /^\s{0,}(\S+)\s{0,}=\s{0,}(.*?)\s\#{0,}.*/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s{0,}(\S+)\s{0,}=\s{0,}(.*?)\s\#{0,}.*/u) 6 1 7
161 6 $self->{'_conf'}->{$1} = $2;
162 6 $count++;
163 }
164 }
165
166 3 close($fh);
167 3 return $count;
168 }
169  
170 =head2 attach
171  
172 =cut
173  
174  
175 2 ▶sub detach {
Subroutine detach: called CC=1 SCAR=0.0
Pod documented
176 2 my $self = shift;
177 2 my $file = File::Spec->catfile($self->path,$self->file);
178 2 $self->_write;
179 2 delete $self->{'_conf'};
180 2 $self->{'attached'}=false;
181 2 return true;
182 }
183  
184 4 ▶sub _write {
Subroutine _write: called CC=3 SCAR=11.0
185 4 my $self = shift;
186 4 my $file = File::Spec->catfile($self->path,$self->file);
187 4 ▶ open(my $fh, ">".$file) or die "FATAL ERROR: unable to open file = $file\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, '>' . $file or die "FATAL ERROR: unable to open file = $file\n" 4 0 4
188 4 ▶ while (my ($k,$v) = each %{$self->{'_conf'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"_conf"};}) 12 4 16
189 12 printf $fh "%s=%s\n",$k,$v;
190 }
191 4 close($fh);
192 }
193  
194 =head2 detach
195  
196 =cut
197  
198 4 ▶sub retrieve {
Subroutine retrieve: called CC=7 SCAR=19.9
Pod documented
199 2 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
200 4 my $self = shift;
201 4 my $k = shift;
202 4 ▶ return undef if (!$self->{'attached'});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"attached"} 0 4 4
203
204 4 ▶ printf STDERR "D[%i] key = %s\n",$$,$k if ($self->debug);
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 4 0 4
205
206 4 ▶ if (defined($self->{'_conf'}))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'_conf'}) { } 4 0 4
207 {
208 4 my %h = %{$self->{'_conf'}};
209 4 my $v;
210 4 ▶ $v = $h{$k} if (defined($h{$k}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $h{$k} 3 1 4
211 4 ▶ printf STDERR "D[%i] val = %s\n",$$,( defined($v) ? $v : "undef") if ($self->debug);
Branch: 2 branches
BranchTrueFalseTotal
if $self->debug 4 0 4
defined $v ? : 3 1 4
212 4 return $v;
213 }
214 else
215 0 { return undef }
216 }
217  
218 =head2 retrieve
219  
220 =cut
221  
222  
223 2 ▶sub update {
Subroutine update: called CC=3 SCAR=11.0
Pod documented
224 2 my $self = shift;
225 2 my $kvp = shift;
226 2 ▶ return undef if (!$self->{'attached'});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"attached"} 0 2 2
227 2 my ($k,$v);
228 2 ▶ while (($k,$v) = each %{$kvp}) { $self->{'_conf'}->{$k} = $v; }
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$kvp;}) 6 2 8
229 2 $self->_write;
230 }
231  
232 =head2 update
233  
234 =cut
235  
236 0 ▶sub delete {
Subroutine delete: not called CC=3 SCAR=24.8
Pod documented
237 0 my $self = shift;
238 0 my $k = shift;
239 0 ▶ if (defined($self->{'conf'}))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'conf'}) { } 0 0 0
240 {
241 0 ▶ if (defined($self->{'_conf'}->{$k}))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'_conf'}{$k}) { } 0 0 0
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<< <landman at scalableinformatics.com> >>
261  
262 =head1 BUGS
263  
264 Please report any bugs or feature requests to C<bug-sysconf at rt.cpan.org>, or through
265 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SysConf>. 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<http://rt.cpan.org/NoAuth/Bugs.html?Dist=SysConf>
285  
286 =item * AnnoCPAN: Annotated CPAN documentation
287  
288 L<http://annocpan.org/dist/SysConf>
289  
290 =item * CPAN Ratings
291  
292 L<http://cpanratings.perl.org/d/SysConf>
293  
294 =item * Search CPAN
295  
296 L<http://search.cpan.org/dist/SysConf/>
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 2 1; # End of SysConf
↑ Summary