blib/lib/GetRc.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package GetRc;
2 require 5.002;
3 require Exporter;
4  
5 #use strict;
6 4 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use UNIVERSAL qw(isa);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 use Carp;
10 use IO::File;
11 use Fcntl qw(:DEFAULT :flock); # import LOCK_* constants
12  
13  
14 ### my initial version was 0.13
15 ### version 0.20 is first OO ( Object Oriented of course )
16 $VERSION = '0.23';
17  
18 @ISA = qw(Exporter);
19  
20 # Items to export into callers namespace by default
21 @EXPORT = qw();
22  
23 # Other items we are prepared to export if requested
24 @EXPORT_OK = qw();
25  
26  
27 sub new {
28 my $type = shift;
29 my $class = ref($type) || $type;
30 croak "Usage $class->new (filename)" if ( @_ != 1 ) ;
31 my $self = {};
32 bless $self, $class;
33  
34 $self->{'filename'} = shift;
35  
36 $self->_init;
37 $self->_find_file;
38  
39 return($self);
40 }
41  
42 sub _find_file {
43 my $self = shift;
44  
45 foreach ( @{$self->{'find_path'}} ) {
46 last if ( $self->{'filename'} =~ /^\//);
47 $self->{'filename'} = "$_".$self->{'filename'},last if ( -e "$_".$self->{'filename'}) ;
48 }
49 $self->dprint("file is " . $self->{'filename'});
50  
51 }
52  
53 sub _init {
54 my $self = shift;
55 my ( $filename,$dirname ) = fileparse($0);
56 $self->{'ifs'} = '\s*=\s*';
57 $self->{'ofs'} = ' = ';
58 $self->{'debug'} = 0;
59 $self->{'multivalues'} = 1;
60 $self->{'lock'} = 1;
61 $self->{'locktimeout'} = 15;
62 $self->{'find_path'} = [
63 "./",
64 (getpwuid($>))[7]."/",
65 $dirname,
66 "../",
67 "/usr/local/etc/",
68 ];
69  
70 bless $self->{'find_path'};
71  
72 return($self);
73 }
74  
75 sub writerc ($\%) {
76 my $self = shift;
77 local *h_input = shift;
78 my ($rc);
79  
80 $self->dprint("join to writerc");
81 ### locking ? uff .. this a bit messy code
82 if ( $self->{'lock'} ) {
83 eval {
84 local $SIG{ALRM} = sub { die "File lock timeouted\n" };
85 alarm($self->{'locktimeout'});
86 $self->dprint("openning file ".$self->{'filename'});
87 $rc = new IO::File $self->{'filename'}, O_CREAT|O_WRONLY|O_TRUNC;
88 croak "Can't open file\n" unless ( defined $rc );
89 $self->dprint("locking file ".$self->{'filename'});
90 flock($rc,LOCK_EX);
91 $self->dprint($self->{'filename'} . " locked");
92 alarm(0);
93 };
94  
95 if ($@) {
96 return(-3),$self->dprint("File lock timeouted\n") if $@ eq "File lock timeouted\n";
97 return(-4),$self->dprint("Can't open file ".$self->{'filename'}) if $@ eq "Can't open file\n";
98 }
99  
100 } else {
101 $self->dprint("openning file ".$self->{'filename'});
102 $rc = new IO::File $self->{'filename'}, O_CREAT|O_WRONLY|O_TRUNC;
103 croak "Can't open file ".$self->{'filename'}." : $!" unless ( defined $rc );
104 }
105  
106 my $separator = $self->{'ofs'};
107  
108 while (($key,$value) = each %h_input) {
109 print $rc "$key${separator}$value\n";
110 print "HEY: $key => $value\n";
111 }
112 flock($rc,LOCK_UN),$self->dprint( $self->{'filename'} ." unlocked") if ( $self->{'lock'} );
113 $rc->close;
114 croak "Can't close file ".$self->{'filename'} .": $1" if $?;
115 $self->dprint( $self->{'filename'} ." closed");
116 return(0);
117  
118 }
119  
120 sub getrc ($\%){
121 my $self = shift;
122 local *h_input = shift;
123 my ( $key, $value, $rc);
124  
125 $self->dprint("join to getrc");
126 $self->dprint("file ".$self->{'filename'}." doesn't exist"),return(-1) unless ( -e $self->{'filename'} );
127 $self->dprint("Can't read file ".$self->{'filename'}),return(-2) unless ( -r $self->{'filename'} || -R $self->{'filename'} );
128  
129 ### locking ? uff .. this a bit messy code
130 if ( $self->{'lock'} ) {
131 eval {
132 local $SIG{ALRM} = sub { die "File lock timeouted\n" };
133 alarm($self->{'locktimeout'});
134 $self->dprint("openning file ".$self->{'filename'});
135 $rc = new IO::File $self->{'filename'}, O_RDONLY;
136 croak "Can't open file ".$self->{'filename'}." : $!" unless ( defined $rc );
137 $self->dprint("locking file ".$self->{'filename'});
138 flock($rc,LOCK_EX);
139 $self->dprint($self->{'filename'} . " locked");
140 alarm(0);
141 };
142  
143 if ($@) {
144 $self->dprint("File lock timeouted\n") if $@ eq "File lock timeouted\n";
145 return(-3);
146 }
147  
148 } else {
149 $self->dprint("openning file ".$self->{'filename'});
150 $rc = new IO::File $self->{'filename'}, O_RDONLY;
151 croak "Can't open file ".$self->{'filename'}." : $!" unless ( defined $rc );
152 }
153  
154 my $separator = $self->{'ifs'};
155  
156 while (<$rc>) {
157 chomp;
158 #### Skip blank text entry fields and comment
159 next if ( /^\s*#/ || /^\s*$/ || /^\s*\;/);
160  
161 #### Allow for multiple line values
162 if (s/\\$//) {
163 $_ .= <$rc>;
164 redo;
165 }
166
167 ($key,$value) = /\s*(.*?)${separator}(.*)/;
168  
169 ### skip empty keys
170 $self->dprint("skip line: $_"),next if ( !defined($value) || !$key );
171
172 #### Allow for multiple values of a single name
173 if ($h_input{"$key"} && $self->{'multivalues'}) {
174 $h_input{"$key"} .= ", " ;
175 $h_input{"$key"} .= $value;
176 } else {
177 $h_input{"$key"} = $value;
178 }
179 }
180 flock($rc,LOCK_UN),$self->dprint( $self->{'filename'} ." unlocked") if ( $self->{'lock'} );
181 $rc->close;
182 croak "Can't close file ".$self->{'filename'} .": $1" if $?;
183 $self->dprint( $self->{'filename'} ." closed");
184 return(0);
185 }
186  
187 sub updaterc ($\%){
188 my $self = shift;
189 local *h_input = shift;
190 my ( $key, $value, $rc, %update_input);
191  
192 $self->dprint("join to updaterc");
193 $self->dprint("WARN: file ".$self->{'filename'}." doesn't exist") unless ( -e $self->{'filename'} );
194 $self->dprint("WARN: Can't read file ".$self->{'filename'}) unless ( -r $self->{'filename'} || -R $self->{'filename'} );
195  
196 ### locking ? uff .. this a bit messy code
197 if ( $self->{'lock'} ) {
198 eval {
199 local $SIG{ALRM} = sub { die "File lock timeouted\n" };
200 alarm($self->{'locktimeout'});
201 $self->dprint("openning file ".$self->{'filename'});
202 $rc = new IO::File $self->{'filename'}, O_RDWR|O_CREAT;
203 croak "Can't open file\n" unless ( defined $rc );
204 $self->dprint("locking file ".$self->{'filename'});
205 flock($rc,LOCK_EX);
206 $self->dprint($self->{'filename'} . " locked");
207 alarm(0);
208 };
209  
210 if ($@) {
211 return(-3),$self->dprint("File lock timeouted\n") if $@ eq "File lock timeouted\n";
212 return(-4),$self->dprint("Can't open file ".$self->{'filename'}) if $@ eq "Can't open file\n";
213
214 }
215  
216 } else {
217 $self->dprint("openning file ".$self->{'filename'});
218 $rc = new IO::File $self->{'filename'}, O_RDWR|O_CREAT;
219 croak "Can't open file ".$self->{'filename'}." : $!" unless ( defined $rc );
220 }
221  
222 my $separator = $self->{'ifs'};
223  
224 while (<$rc>) {
225 chomp;
226 #### Skip blank text entry fields and comment
227 next if ( /^\s*#/ || /^\s*$/ || /^\s*\;/);
228  
229 #### Allow for multiple line values
230 if (s/\\$//) {
231 $_ .= <$rc>;
232 redo;
233 }
234
235 ($key,$value) = /\s*(.*?)${separator}(.*)/;
236  
237 ### skip empty keys
238 $self->dprint("skip line: $_"),next if ( !defined($value) || !$key );
239
240 #### Allow for multiple values of a single name
241 if ($update_input{"$key"} && $self->{'multivalues'}) {
242 $update_input{"$key"} .= ", " ;
243 $update_input{"$key"} .= $value;
244 } else {
245 $update_input{"$key"} = $value;
246 }
247 }
248  
249 while (($key,$value) = each %h_input) {
250 $update_input{"$key"} = $value;
251 }
252  
253 $separator = $self->{'ofs'};
254  
255 seek($rc,0,0);
256 truncate($rc,0);
257  
258 while (($key,$value) = each %update_input) {
259 print $rc "$key${separator}$value\n";
260 }
261  
262  
263 flock($rc,LOCK_UN),$self->dprint( $self->{'filename'} ." unlocked") if ( $self->{'lock'} );
264 $rc->close;
265 croak "Can't close file ".$self->{'filename'} .": $1" if $?;
266 $self->dprint( $self->{'filename'} ." closed");
267 return(0);
268 }
269  
270 sub configure {
271 my $self = shift;
272 my (%arg) = @_;
273  
274 foreach (keys %arg) {
275 $self->{"$_"} = $arg{$_};
276 }
277  
278 bless $self->{'find_path'};
279 return($self);
280 }
281  
282 sub dprint {
283 my $self = shift;
284 my $debug_output = shift;
285 my $filename = $self->{'filename'};
286 return(0) unless $self->{'debug'};
287 print STDERR <<EOM;
288 ==> PID:$$\t$debug_output
289
290 EOM
291 }
292  
293 sub AUTOLOAD {
294 my $self = shift;
295 my $value = shift;
296 my ($name) = $AUTOLOAD;
297  
298 ($name) = ( $name =~ /^.*::(.*)/);
299  
300 $self->{$name} = $value if ( defined $value );
301  
302 ### find file location
303 $self->_find_file if ( $value && ($name eq 'filename'));
304  
305 if ( ref($self->{$name}) =~ /ARRAY/ ) {
306 return(@{$self->{$name}});
307 } elsif (isa($self->{$name}, 'ARRAY')) {
308 if ( wantarray ) {
309 return(@{$self->{$name}});
310 } else {
311 return($self->{$name});
312 }
313 } else {
314 return($self->{$name});
315 }
316 }
317  
318 sub push {
319 my $self = shift;
320 my $value = shift;
321  
322 return() unless (ref($self) !~ /ARRAY/ );
323  
324 push @{$self},$value;
325 return($self);
326 }
327  
328 sub unshift {
329 my $self = shift;
330 my $value = shift;
331  
332 return() unless (ref($self) !~ /ARRAY/ );
333  
334 unshift @{$self},$value;
335 return($self);
336 }
337  
338 sub pop {
339 my $self = shift;
340  
341 return() unless (ref($self) !~ /ARRAY/ );
342  
343 pop @{$self};
344 }
345  
346 sub shift {
347 my $self = shift;
348  
349 return() unless (ref($self) !~ /ARRAY/ );
350  
351 shift @{$self};
352 }
353  
354 sub DESTROY {}
355  
356  
357 1;
358 __END__
↑ Summary