lib/File/Data.pm

filter: Statementstmt 83.8% 375 / 447 Branchbran 58.9% 132 / 224 Conditioncond 42.6% 35 / 82 MC/DCmcdc 4.7% 2 / 42 Subroutinesub 82.9% 39 / 47 Podpod 33.3% 5 / 15 totaltotal 68.6% 588 / 857 CC 137 SCAR 21.7
CC
137
Cov
72%
_error
33.3
dummy
20.0
REPLACE
14.9
CRAP 8.8
↑ Summary
1 package File::Data;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Fcntl qw(:flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 # use Tie::File; # <- todo
11 # use File::stat;
12 1 ▶use vars qw(@ISA $VERSION $AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 $VERSION = do { my @r = (q$Revision: 1.20 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
14 1 $| = 1;
15  
16 =head1 NAME
17  
18 File::Data - interface to file data
19  
20 =head1 DESCRIPTION
21  
22 Wraps all the accessing of a file into a convenient set of calls for
23 reading and writing data, including a simple regex interface.
24  
25 Note that the file needs to exist prior to using this module!
26  
27 See L<new()>
28  
29 =head1 SYNOPSIS
30  
31 =over 4
32  
33 use strict;
34  
35 use File::Data;
36  
37 my $o_dat = File::Data->new('./t/example');
38  
39 $o_dat->write("complete file contents\n");
40  
41 $o_dat->prepend("first line\n"); # line 0
42  
43 $o_dat->append("original second (last) line\n");
44  
45 $o_dat->insert(2, "new second line\n"); # inc. zero!
46  
47 $o_dat->replace('line', 'LINE');
48  
49 print $o_dat->READ;
50  
51 Or, perhaps more seriously :-}
52  
53 my $o_sgm = File::Data->new('./sgmlfile');
54  
55 print "new SGML data: ".$o_sgm->REPLACE(
56 '\<\s*((?i)tag)\s*\>\s*((?s).*)\s*\<\s*((?i)\s*\/\s*tag)\s*\>',
57 qq|<tag>key="val"</tag>|,
58 ) if $o_sgm;
59  
60 See L<METHODS> and L<EXAMPLES>.
61  
62 =back
63  
64 =head1 IMPORTANT
65  
66 lowercase method calls return the object itself, so you can chain calls.
67  
68 my $o_obj = $o_dat->read; # ! <= object !
69  
70 UPPERCASE method calls return the data relevant to the operation.
71  
72 my @data = $o_dat->READ; # ! <= data !
73  
74 While this may occasionally be frustrating, using the B<principle of
75 least surprise>, it is at least consistent.
76  
77 See L<do>
78  
79 =head1 EXPLANATION
80  
81 =over 4
82  
83 The idea is to standardise accessing of files for repetitive and straight
84 forward tasks, and remove the repeated and therefore error prone file
85 access I have seen in many sites, where varying, (with equivalently
86 varying success), methods are used to achieve essentially the same result
87 - a simple search and replace and/or a regex match.
88  
89 Approaches to opening and working with files vary so much, where
90 one person may wish to know if a file exists, another wishes to know
91 whether the target is a file, or if it is readable, or writable and so on.
92 Sometimes, in production code even (horror), file's are opened without any
93 checks of whether the open was successful. Then there's a loop through
94 each line to find the first or many patterns to read and/or replace.
95 With a failure, normally the only message is 'permission denied', is
96 that read or write access, does the file even exist? etc.
97  
98 This module attempts to provide a plain/generic interface to accessing
99 a file's data. This will not suit every situation, but I have included
100 some examples which will hopefully demonstrate that it may be used
101 in situations where people would normally go through varying and
102 inconsistent, (and therefore error-prone), procedures - to get at the
103 same data.
104  
105 Theoretically you can mix and match your read and writes so long as you
106 don't open read-only.
107  
108 my $o_dat = File::Data->new($file);
109  
110 my $i_snrd = $o_dat->append($append)->REPLACE($search, $replace);
111  
112 print $o_dat->READ;
113  
114 If you want to apply the same regex, or insert/prepend/replacement/whatever
115 mechanism, to many different files, then the neatest solution may be to do
116 something like the following:
117  
118 foreach my $file ( @list_of_file_names ) {
119 my $o_dat = File::Data->new($file);
120  
121 my $i_snrd = $o_dat->append($append)->REPLACE($search, $replace);
122  
123 print $o_dat->READ;
124 }
125  
126 One last thing - I'm sure this could be made more efficient, and I'd be
127 receptive to any suggestions to that effect. Note though that the intention has
128 been to create a simple and consistent interface, rather than a complicated
129 one.
130  
131 =back
132  
133 =cut
134  
135 # Methods we like:
136 # ================================================================
137 #
138 1 my @_METHODS = qw(append insert prepend read replace return search write);
139 1 my $_METHODS = join('|', @_METHODS);
140  
141 =head1 METHODS
142  
143 =over 4
144  
145 =item new
146  
147 Create a new File::Data object (default read-write).
148  
149 my $o_rw = File::Data->new($filename); # read-write
150  
151 my $o_ro = File::Data->new($filename, 'ro'); # read-only
152  
153 Each file should have it's own discrete object.
154  
155 Note that if you open a file read-only and then attempt to write to it,
156 that will be regarded as an error, even if you change the permissions
157 in the meantime.
158  
159 Further: The file B<must> exist before successful use of this method
160 is possible. This is B<not> a replacement for modules which create and
161 delete files, this is purely designed as an interface to the B<data>
162 of existing files. A B<create> function is a future possibility.
163  
164 Look in L<EXAMPLES> for a more complete explanation of possible arguments
165 to the B<new()> method
166  
167 =cut
168  
169 556 ▶sub new {
Subroutine new: called CC=5 SCAR=16.4
Pod documented
170 556 my $class = shift;
171 556 my $file = shift;
172 556 ▶ my $perms = shift || $File::Data::PERMISSIONS;
Condition66%2/3
shift() || $File::Data::PERMISSIONS
l!l&&r!l&&!r
255310
MC/DC0%0/2
shift() || $File::Data::PERMISSIONS
shift() $File::Data::PERMISSIONS
Truth table66%2/3
A || B
A= shift()B= $File::Data::PERMISSIONS
ABresult
1X1
011
000
173 556 ▶ my $h_err = shift || {};
Condition50%1/2
shift() || {}
l!l
0556
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
174  
175 556 my $self = bless({
176 '_err' => {},
177 '_var' => {
178 'backup' => 0,
179 'limbo' => '',
180 'state' => 'init',
181 'writable' => 0,
182 },
183 }, $class);
184  
185 556 ▶ $self->_debug("file($file), perm($perms), h_err($h_err)") if $File::Data::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Data::DEBUG 0 556 556
186 556 my $i_ok = $self->_init($file, $perms, $h_err);
187  
188 556 ▶ return $i_ok == 1 ? $self : undef;
Branch: 1 branch
BranchTrueFalseTotal
$i_ok == 1 ? : 152 404 556
189 }
190  
191 =item read
192  
193 Read all data from file
194  
195 $o_dat = $o_dat->read; # !
196  
197 my @data = $o_dat->READ;
198  
199 =cut
200  
201 2 ▶sub READ {
Subroutine READ: called CC=3 SCAR=11.2
202 2 my $self = shift;
203  
204 2 $self->_enter('read');
205 2 ▶ $self->_debug('in: ') if $File::Data::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Data::DEBUG 0 2 2
206  
207 2 my @ret = $self->_read;
208  
209 2 ▶ $self->_debug('out: '.Dumper(\@ret)) if $File::Data::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Data::DEBUG 0 2 2
210 2 $self->_leave('read');
211  
212 2 return @ret;
213 };
214  
215 =item _internal
216  
217 read
218  
219 does this...
220  
221 =cut
222  
223 2 ▶sub _read { #
Subroutine _read: called CC=2 SCAR=7.0
224 2 my $self = shift;
225  
226 2 my $FH = $self->_fh;
227 2 $FH->seek(0, 0);
228 #
229 2 my @ret = <$FH>;
230  
231 2 ▶ return ($File::Data::REFERENCE) ? \@ret : @ret;
Branch: 1 branch
BranchTrueFalseTotal
$File::Data::REFERENCE ? : 0 2 2
232 };
233  
234 =item write
235  
236 Write data to file
237  
238 my $o_dat = $o_dat->WRITE; # !
239  
240 my @written = $o_dat->write;
241  
242 =cut
243  
244 5 ▶sub WRITE {
Subroutine WRITE: called CC=5 SCAR=16.2
245 5 my $self = shift;
246 5 my @args = @_;
247 5 my @ret = ();
248  
249 5 $self->_enter('write');
250 5 ▶ $self->_debug('in: '.Dumper(\@args)) if $File::Data::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Data::DEBUG 0 5 5
251  
252 5 ▶ if ($self->_writable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_writable) 4 1 5
253 4 my $FH = $self->_fh;
254 4 $FH->truncate(0);
255 4 $FH->seek(0, 0);
256 4 @ret = $self->_write(@args);
257 }
258  
259 5 ▶ $self->_debug('out: '.Dumper(\@ret)) if $File::Data::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Data::DEBUG 0 5 5
260 5 $self->_leave('write');
261  
262 5 ▶ return ($File::Data::REFERENCE) ? \@ret : @ret;
Branch: 1 branch
BranchTrueFalseTotal
$File::Data::REFERENCE ? : 0 5 5
263 };
264  
265 12 ▶sub _write { #
Subroutine _write: called CC=6 SCAR=18.7
266 12 my $self = shift;
267 12 my @ret = ();
268  
269 12 my $FH = $self->_fh;
270 12 my $pos = $FH->tell;
271 12 ▶ $self->_debug("writing at curpos: $pos") if $File::Data::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Data::DEBUG 0 12 12
272 12 foreach (@_) {
273 44 ▶ push(@ret, $_) if print $FH $_;
Branch: 1 branch
BranchTrueFalseTotal
if print $FH $_ 44 0 44
274 44 ▶ $self->_debug("wrote -->$_<--") if $File::Data::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Data::DEBUG 0 44 44
275 }
276  
277 12 ▶ return ($File::Data::REFERENCE) ? \@ret : @ret;
Branch: 1 branch
BranchTrueFalseTotal
$File::Data::REFERENCE ? : 0 12 12
278 };
279  
280 =item prepend
281  
282 Prepend to file
283  
284 my $o_dat = $o_dat->prepen(\@lines); # !
285  
286 my @prepended = $o_dat->prepend(\@lines);
287  
288 =cut
289  
290 3 ▶sub PREPEND {
Subroutine PREPEND: called CC=6 SCAR=18.2
Pod undocumented
291 3 my $self = shift;
292 3 my @ret = ();
293  
294 3 $self->_enter('prepend');
295 3 ▶ $self->_debug('in: '.Dumper(@_)) if $File::Data::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Data::DEBUG 0 3 3
296  
297 3 ▶ if ($self->_writable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_writable) 2 1 3
298 2 my $FH = $self->_fh;
299 2 $FH->seek(0, 0);
300 2 my @data = <$FH>;
301 2 $FH->truncate(0);
302 2 $FH->seek(0, 0);
303 2 ▶ @ret = @_ if $self->_write(@_, @data);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_write(@_, @data) 2 0 2
304 }
305  
306 3 ▶ $self->_debug('out: '.Dumper(\@ret)) if $File::Data::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Data::DEBUG 0 3 3
307 3 $self->_leave('prepend');
308  
309 3 ▶ return ($File::Data::REFERENCE) ? \@ret : @ret;
Branch: 1 branch
BranchTrueFalseTotal
$File::Data::REFERENCE ? : 0 3 3
310 };
311  
312 =item insert
313  
314 Insert data at line number, starting from '0'
315  
316 my $o_dat = $o_dat->insert($i_lineno, \@lines); # !
317  
318 my @inserted = $o_dat->INSERT($i_lineno, \@lines);
319  
320 =cut
321  
322 3 ▶sub INSERT {
Subroutine INSERT: called CC=12 SCAR=26.0
Pod undocumented
323 3 my $self = shift;
324 3 my $line = shift;
325 3 my @ret = ();
326  
327 3 $self->_enter('insert');
328 3 ▶ $self->_debug('in: '.Dumper(\@_)) if $File::Data::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Data::DEBUG 0 3 3
329  
330 3 ▶ if ($line !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $line =~ /^\d+$/) { } 1 2 3
331 1 $self->_error("can't go to non-numeric line($line)");
332 } else {
333 2 ▶ if ($self->_writable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_writable) 2 0 2
334 2 my $FH = $self->_fh;
335 2 $FH->seek(0, 0);
336 2 my $i_cnt = -1;
337 2 my @pre = ();
338 2 my @post = ();
339 2 ▶ while (<$FH>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $FH)) 10 2 12
340 10 $i_cnt++; # 0..n
341 10 my $pos = $FH->tell;
342 10 ▶ if ($i_cnt < $line) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i_cnt < $line) { } 4 6 10
elsif ($i_cnt >= $line) { } 6 0 6
343 4 push(@pre, $_);
344 } elsif ($i_cnt >= $line) {
345 6 push(@post, $_);
346 }
347 }
348 2 $i_cnt++;
349 2 ▶ if (!($i_cnt >= $line)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $i_cnt >= $line) { } 0 2 2
350 0 ▶ my $s = ($i_cnt == 1) ? '' : 's';
Branch: 1 branch
BranchTrueFalseTotal
$i_cnt == 1 ? : 0 0 0
351 0 $self->_error("couldn't insert($line, ...) while only $i_cnt line$s in file");
352 } else {
353 2 $FH->truncate(0);
354 2 $FH->seek(0, 0);
355 2 ▶ @ret = @_ if $self->_write(@pre, @_, @post);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_write(@pre, @_, @post) 2 0 2
356 }
357 }
358 }
359  
360 3 ▶ $self->_debug('out: '.Dumper(\@ret)) if $File::Data::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Data::DEBUG 0 3 3
361 3 $self->_leave('insert');
362  
363 3 ▶ return ($File::Data::REFERENCE) ? \@ret : @ret;
Branch: 1 branch
BranchTrueFalseTotal
$File::Data::REFERENCE ? : 0 3 3
364 }
365  
366 =item append
367  
368 Append to file
369  
370 my $o_dat = $o_dat->append(\@lines); # !
371  
372 my @appended = $o_dat->APPEND(\@lines);
373  
374 =cut
375  
376 3 ▶sub APPEND {
Subroutine APPEND: called CC=6 SCAR=18.3
Pod undocumented
377 3 my $self = shift;
378 3 my @ret = ();
379  
380 3 $self->_enter('append');
381 3 ▶ $self->_debug('in: '.Dumper(\@_)) if $File::Data::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Data::DEBUG 0 3 3
382  
383 3 ▶ if ($self->_writable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_writable) 2 1 3
384 2 my $FH = $self->_fh;
385 2 $FH->seek(0, 2);
386 2 ▶ @ret = @_ if $self->_write(@_);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_write(@_) 2 0 2
387 }
388  
389 3 ▶ $self->_debug('out: '.Dumper(\@ret)) if $File::Data::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Data::DEBUG 0 3 3
390 3 $self->_leave('append');
391  
392 3 ▶ return ($File::Data::REFERENCE) ? \@ret : @ret;
Branch: 1 branch
BranchTrueFalseTotal
$File::Data::REFERENCE ? : 0 3 3
393 };
394  
395 =item search
396  
397 Retrieve data out of a file, simple list of all matches found are returned.
398  
399 Note - you must use capturing parentheses for this to work!
400  
401 my $o_dat = $o_dat->search('^(.*\@.*)$'); # !
402  
403 my @addrs = $o_dat->SEARCH('^(.*\@.*)$');
404  
405 my @names = $o_dat->SEARCH('^(?:[^:]:){4}([^:]+):');
406  
407 =cut
408  
409 2 ▶sub SEARCH {
Subroutine SEARCH: called CC=11 SCAR=24.4
Pod undocumented
410 2 my $self = shift;
411 2 my $search = shift;
412 2 my @ret = ();
413  
414 2 $self->_enter('search');
415 2 ▶ $self->_debug("in: $search") if $File::Data::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Data::DEBUG 0 2 2
416  
417 2 ▶ if ($search !~ /.+/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $search =~ /.+/) { } 0 2 2
418 0 $self->_error("no search($search) given");
419 } else {
420 2 my $file = $self->_var('filename');
421 2 my $FH = $self->_fh;
422 2 $FH->seek(0, 0);
423 2 my $i_cnt = 0;
424 2 ▶ if ($File::Data::STRING) { # default
Branch: 1 branch
BranchTrueFalseTotal
if ($File::Data::STRING) { } 1 1 2
425 1 my $orig = $/; $/ = undef; # slurp
426 1 my $data = <$FH>; $/ = $orig;
427 1 ▶ $self->_debug("looking at data($data)") if $File::Data::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Data::DEBUG 0 1 1
428 1 @ret = ($data =~ /$search/g);
429 1 $i_cnt = ($data =~ tr/\n/\n/);
430 } else {
431 1 ▶ while (<$FH>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $FH)) 7 1 8
432 7 ▶ $self->_debug("looking at line($_)") if $File::Data::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Data::DEBUG 0 7 7
433 7 my $line = $_;
434 # push(@ret, ($line =~ /$search/));
435 7 ▶ push(@ret, $line) if ($line =~ /$search/);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /$search/ 3 4 7
436 7 $i_cnt++;
437 }
438 }
439 2 ▶ if (scalar(@ret) >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @ret >= 1) 2 0 2
440 2 $self->_debug("search($search) in file($file) lines($i_cnt) result(@ret)");
441 }
442 }
443  
444 2 ▶ $self->_debug('out: '.Dumper(\@ret)) if $File::Data::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Data::DEBUG 0 2 2
445 2 $self->_leave('search');
446  
447 2 ▶ return ($File::Data::REFERENCE) ? \@ret : @ret;
Branch: 1 branch
BranchTrueFalseTotal
$File::Data::REFERENCE ? : 0 2 2
448 }
449  
450 =item replace
451  
452 Replace data in a 'search and replace' manner, returns the final data.
453  
454 my $o_dat = $o_dat->replace($search, $replace); # !
455  
456 my @data = $o_dat->REPLACE($search, $replace);
457  
458 my @data = $o_dat->REPLACE(
459 q|\<a href=(['"])([^$1]+)?$1| => q|'my.sales.com'|,
460 );
461  
462 This is B<simple>, in that you can do almost anything in the B<search> side,
463 but the B<replace> side is a bit more restricted, as we can't effect the
464 replacement modifiers on the fly.
465  
466 If you really need this, perhaps B<(?{})> can help?
467  
468 =cut
469  
470 3 ▶sub REPLACE {
Subroutine REPLACE: called CC=14 SCAR=27.0
Pod undocumented
471 3 my $self = shift;
472 3 my %args = @_;
473 3 my @ret = ();
474  
475 3 $self->_enter('replace');
476 3 ▶ $self->_debug('in: '.Dumper(\%args)) if $File::Data::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Data::DEBUG 0 3 3
477  
478 3 ▶ if ($self->_writable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_writable) 2 1 3
479 2 my $file = $self->_var('filename');
480 2 my $FH = $self->_fh;
481 2 $FH->seek(0, 0);
482 2 my $i_cnt = 0;
483 SEARCH:
484 2 foreach my $search (keys %args) {
485 2 my $replace = $args{$search};
486 2 ▶ if ($File::Data::STRING) { # default
Branch: 1 branch
BranchTrueFalseTotal
if ($File::Data::STRING) { } 1 1 2
487 1 my $orig = $/; $/ = undef; # slurp
488 1 my $data = <$FH>; $/ = $orig;
489 1 ▶ $self->_debug("initial ($data)") if $File::Data::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Data::DEBUG 0 1 1
490 1 ▶ if (($i_cnt = ($data =~ s/$search/$replace/g))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i_cnt = $data =~ s/$search/$replace/g) { } 1 0 1
491 1 @ret = $data;
492 } else {
493 0 print "unable($i_cnt) to search($search) and replace($replace)\n";
494 }
495 } else {
496 1 ▶ while (<$FH>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $FH)) 7 1 8
497 7 ▶ $self->_debug("initial line($_)") if $File::Data::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Data::DEBUG 0 7 7
498 7 my $line = $_;
499 7 ▶ if ($line =~ s/$search/$replace/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ s/$search/$replace/) 3 4 7
500 3 $i_cnt++;
501 }
502 7 push(@ret, $line);
503 }
504 }
505 2 ▶ if (scalar(@ret) >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @ret >= 1) 2 0 2
506 2 $FH->seek(0, 0);
507 2 $FH->truncate(0);
508 2 $FH->seek(0, 0);
509 2 @ret = $self->_write(@ret);
510 }
511 2 ▶ if (!($i_cnt >= 1)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($i_cnt >= 1) 0 2 2
512 0 $self->_debug("nonfulfilled search($search) and replace($replace) in file($file)");
513 }
514 }
515 }
516  
517 3 ▶ $self->_debug('out: '.Dumper(\@ret)) if $File::Data::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Data::DEBUG 0 3 3
518 3 $self->_leave('replace');
519  
520 3 ▶ return ($File::Data::REFERENCE) ? \@ret : @ret;
Branch: 1 branch
BranchTrueFalseTotal
$File::Data::REFERENCE ? : 0 3 3
521 }
522  
523 =item xreturn
524  
525 Returns the product of the given (or last) B<do()>, undef on failure.
526  
527 my $o_dat = $o_dat->prepend($A)->append($b)->return('prepend'); # !
528  
529 my @prepended = $o_dat->prepend($A)->append($b)->RETURN('prepend');
530  
531 my @appended = $o_dat->prepend($A)->append($b)->RETURN; # like read()
532  
533 =cut
534  
535 5 ▶sub RETURN {
Subroutine RETURN: called CC=4 SCAR=17.3
Pod undocumented
536 5 my $self = shift;
537 5 ▶ my $call = uc(shift) || $self->_var('last');
Condition33%1/3
uc shift() || $self->_var('last')
l!l&&r!l&&!r
500
MC/DC0%0/2
uc shift() || $self->_var('last')
uc shift() $self->_var('last')
Truth table33%1/3
A || B
A= uc shift()B= $self->_var('last')
ABresult
1X1
011
000
538  
539 5 ▶ if ((defined($self->{'_var'}{$call}) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'_var'}{$call} and ref $self->{'_var'}{$call} eq 'ARRAY') { } 5 0 5
Condition33%1/3
defined $self->{"_var"}{$call} and ref $self->{"_var"}{$call} eq "ARRAY"
!ll&&!rl&&r
005
MC/DC0%0/2
defined $self->{"_var"}{$call} and ref $self->{"_var"}{$call} eq "ARRAY"
defined $self->{"_var"}{$call} ref $self->{"_var"}{$call} eq "ARRAY"
Truth table33%1/3
A and B
A= defined $self->{"_var"}{$call}B= ref $self->{"_var"}{$call} eq "ARRAY"
ABresult
0X0
100
111
540 ref($self->{'_var'}{$call}) eq 'ARRAY'
541 )) {
542 5 return @{$self->_var($call)};
543 } else {
544 0 $self->_debug("not returning invalid call($call) ref($self->{'_var'}{$call})");
545 0 return undef;
546 }
547 }
548  
549 =item create
550  
551 placeholder - unsupported
552  
553 =cut
554  
555 0 ▶sub create {
Subroutine create: not called CC=1 SCAR=6.9
Pod documented
556 0 my $self = shift;
557  
558 0 $self->_error("unsupported call: __FILE__(@_)");
559  
560 0 return ();
561 }
562  
563 =item delete
564  
565 placeholder - unsupported
566  
567 =cut
568  
569 0 ▶sub delete {
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
570 0 my $self = shift;
571  
572 0 $self->_error("unsupported call: __FILE__(@_)");
573  
574 0 return ();
575 }
576  
577 =item close
578  
579 Close the file
580  
581 my $i_closed = $o_dat->close; # 1|0
582  
583 =cut
584  
585 0 ▶sub close {
Subroutine close: not called CC=1 SCAR=6.9
Pod documented
586 0 my $self = shift;
587  
588 0 return $self->_close;
589 }
590  
591  
592 =item info
593  
594 placeholder - unsupported
595  
596 =cut
597  
598 # Returns File::stat object for the file.
599  
600 # print 'File size: '.$o_dat->stat->size;
601  
602 0 ▶sub xFSTAT {
Subroutine xFSTAT: not called CC=2 SCAR=17.9
Pod undocumented
603 0 my $self = shift;
604 0 ▶ my $file = shift || '_';
Condition0%0/2
shift() || '_'
l!l
00
MC/DC0%0/1
shift() || '_'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
605  
606 # print "file($file) stat: ".Dumper(stat($file));
607  
608 # return stat($file);
609  
610 0 return ();
611 }
612  
613 0 ▶sub xfstat {
Subroutine xfstat: not called CC=2 SCAR=17.9
Pod undocumented
614 0 my $self = shift;
615 0 ▶ my $file = shift || '_';
Condition0%0/2
shift() || '_'
l!l
00
MC/DC0%0/1
shift() || '_'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
616  
617 # print "file($file) stat: ".Dumper(stat($file));
618  
619 # stat($file);
620  
621 0 return ();
622 }
623  
624 0 ▶sub dummy {
Subroutine dummy: not called CC=4 SCAR=30.0
Pod undocumented
625 0 my $self = shift;
626 0 my %args = @_;
627 0 my @ret = ();
628  
629 0 $self->_enter('dummy');
630 0 ▶ $self->_debug('in: '.Dumper(\%args)) if $File::Data::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Data::DEBUG 0 0 0
631  
632 # if ($self->_writable) {
633 #
634 # $FH->seek(0, 2);
635 # }
636  
637 0 ▶ $self->_debug('out: '.Dumper(\@ret)) if $File::Data::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Data::DEBUG 0 0 0
638 0 $self->_leave('dummy');
639  
640 0 ▶ return ($File::Data::REFERENCE) ? \@ret : @ret;
Branch: 1 branch
BranchTrueFalseTotal
$File::Data::REFERENCE ? : 0 0 0
641 }
642  
643 =back
644  
645 =cut
646  
647 # ================================================================
648  
649 =head1 VARIABLES
650  
651 Various variables may be set affecting the behaviour of the module.
652  
653 =over 4
654  
655 =item $File::Data::DEBUG
656  
657 Set to 0 (default) or 1 for debugging information to be printed on STDOUT.
658  
659 $File::Data::DEBUG = 1;
660  
661 Alternatively set to a regex of any of the prime methods to debug them individually.
662  
663 $File::Data::DEBUG = '(ap|pre)pend';
664  
665 =cut
666  
667 1 ▶$File::Data::DEBUG ||= $ENV{'File_Data_DEBUG'} || 0;
Condition40%2/5
$File::Data::DEBUG ||= $ENV{"File_Data_DEBUG"} || 0
l!l&&r!l&&!r
001
$ENV{'File_Data_DEBUG'} || 0
l!l
01
MC/DC0%0/2
$File::Data::DEBUG ||= $ENV{"File_Data_DEBUG"} || 0
$File::Data::DEBUG $ENV{'File_Data_DEBUG'}
Truth table33%1/3
A ||= B
A= $File::Data::DEBUGB= $ENV{'File_Data_DEBUG'}
ABresult
1X1
011
000
668 # $File::Data::DEBUG = 1; #
669  
670 =item $File::Data::FATAL
671  
672 Will die if there is any failure in accessing the file, or reading the data.
673  
674 Default = 0 (don't die - just warn);
675  
676 $File::Data::FATAL = 1; # die
677  
678 =cut
679  
680 1 ▶$File::Data::FATAL ||= $ENV{'File_Data_FATAL'} || 0;
Condition40%2/5
$File::Data::FATAL ||= $ENV{"File_Data_FATAL"} || 0
l!l&&r!l&&!r
001
$ENV{'File_Data_FATAL'} || 0
l!l
01
MC/DC0%0/2
$File::Data::FATAL ||= $ENV{"File_Data_FATAL"} || 0
$File::Data::FATAL $ENV{'File_Data_FATAL'}
Truth table33%1/3
A ||= B
A= $File::Data::FATALB= $ENV{'File_Data_FATAL'}
ABresult
1X1
011
000
681  
682 =item $File::Data::REFERENCE
683  
684 Will return a reference, not a list, useful with large files.
685  
686 Default is 0, ie; methods normally returns a list. There may be an argument to
687 make returns work with references by default, feedback will decide.
688  
689 $File::Data::REFERENCE = 1;
690  
691 my $a_ref = $o_dat->search('.*');
692  
693 print "The log: \n".@{ $a_ref };
694  
695 =cut
696  
697 1 ▶$File::Data::REFERENCE ||= $ENV{'File_Data_REFERENCE'} || 0;
Condition40%2/5
$File::Data::REFERENCE ||= $ENV{"File_Data_REFERENCE"} || 0
l!l&&r!l&&!r
001
$ENV{'File_Data_REFERENCE'} || 0
l!l
01
MC/DC0%0/2
$File::Data::REFERENCE ||= $ENV{"File_Data_REFERENCE"} || 0
$File::Data::REFERENCE $ENV{'File_Data_REFERENCE'}
Truth table33%1/3
A ||= B
A= $File::Data::REFERENCEB= $ENV{'File_Data_REFERENCE'}
ABresult
1X1
011
000
698  
699  
700 =item $File::Data::SILENT
701  
702 Set to something other than zero if you don't want error messages ?-\
703  
704 $File::Data::SILENT = 0; # per line
705  
706 =cut
707  
708 1 ▶$File::Data::SILENT ||= $ENV{'File_Data_SILENT'} || 0;
Condition40%2/5
$File::Data::SILENT ||= $ENV{"File_Data_SILENT"} || 0
l!l&&r!l&&!r
001
$ENV{'File_Data_SILENT'} || 0
l!l
01
MC/DC0%0/2
$File::Data::SILENT ||= $ENV{"File_Data_SILENT"} || 0
$File::Data::SILENT $ENV{'File_Data_SILENT'}
Truth table33%1/3
A ||= B
A= $File::Data::SILENTB= $ENV{'File_Data_SILENT'}
ABresult
1X1
011
000
709  
710  
711 =item $File::Data::STRING
712  
713 Where regex's are used, default behaviour is to treate the entire file as a
714 single scalar string, so that, for example, B<(?ms:...)> matches are effective.
715  
716 Unset if you don't want this behaviour.
717  
718 $File::Data::STRING = 0; # per line
719  
720 =cut
721  
722 1 ▶$File::Data::STRING ||= $ENV{'File_Data_STRING'} || 1;
Condition40%2/5
$File::Data::STRING ||= $ENV{"File_Data_STRING"} || 1
l!l&&r!l&&!r
010
$ENV{'File_Data_STRING'} || 1
l!l
01
MC/DC0%0/2
$File::Data::STRING ||= $ENV{"File_Data_STRING"} || 1
$File::Data::STRING $ENV{'File_Data_STRING'}
Truth table0%0/3
A ||= B
A= $File::Data::STRINGB= $ENV{'File_Data_STRING'}
ABresult
1X1
011
000
723  
724  
725 =item $File::Data::PERMISSIONS
726  
727 File will be opened read-write (B<insert()> compatible) unless this
728 variable is set explicitly or given via B<new()>. In either case,
729 unless it is one of our valid permission B<keys> declared below,
730 it will be passed on to B<FileHandle> and otherwise not modified.
731 We don't support fancy permission sets, just read or write.
732  
733 Read-only permissions may be explicitly set using one of these B<keys>:
734  
735 $File::Data::PERMISSIONS = 'ro'; # or readonly or <
736  
737 Or, equivalently, for read-write (default):
738  
739 $File::Data::PERMISSIONS = 'rw'; # or readwrite or +<
740  
741 Note that it makes no sense to have an 'append only' command (>>),
742 we'd have to disable all of write, search and replace, and insert,
743 etc. in that case - just use the B<append()> method only.
744  
745 This is a KISS-compatible module remember?
746  
747 =cut
748  
749 1 ▶$File::Data::PERMISSIONS ||= $ENV{'File_Data_PERMISSIONS'} || '+<';
Condition40%2/5
$File::Data::PERMISSIONS ||= $ENV{"File_Data_PERMISSIONS"} || "+<"
l!l&&r!l&&!r
010
$ENV{'File_Data_PERMISSIONS'} || '+<'
l!l
01
MC/DC0%0/2
$File::Data::PERMISSIONS ||= $ENV{"File_Data_PERMISSIONS"} || "+<"
$File::Data::PERMISSIONS $ENV{'File_Data_PERMISSIONS'}
Truth table0%0/3
A ||= B
A= $File::Data::PERMISSIONSB= $ENV{'File_Data_PERMISSIONS'}
ABresult
1X1
011
000
750  
751  
752 =back
753  
754 # ================================================================
755  
756 =head1 SPECIAL
757  
758 ...
759  
760 =over 4
761  
762 =item AUTOLOAD
763  
764 Any unrecognised function will be passed to the FileHandle object for final
765 consideration, behaviour is then effectively 'o_dat ISA FileHandle'.
766  
767 $o_dat->truncate;
768  
769 =cut
770  
771 6 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=4 SCAR=17.9
772 6 my $self = shift;
773 6 ▶ return if $AUTOLOAD =~ /::DESTROY$/o; # protection
Branch: 1 branch
BranchTrueFalseTotal
if $File::Data::AUTOLOAD =~ /::DESTROY$/o 0 6 6
774  
775 6 my $meth = $AUTOLOAD;
776 6 $meth =~ s/.+::([^:]+)$/$1/;
777  
778 6 ▶ if ($meth =~ /^($_METHODS)$/io) { # convenience
Branch: 1 branch
BranchTrueFalseTotal
if ($meth =~ /^($_METHODS)$/io) { } 6 0 6
779 6 $self->_debug("rerouting: $meth(@_)");
780 6 return $self->do(uc($meth), @_); # <-
781 # return $self->do(lc($meth), @_);
782 } else { # or fallback
783 0 my $FH = $self->_fh;
784 0 ▶ if ($FH->can($meth)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($FH->can($meth)) { } 0 0 0
785 0 return $FH->$meth(@_); # <-
786 } else {
787 0 $DB::single=2; #
788 0 return $self->_error("no such method($meth)!"); # <-
789 }
790 }
791 }
792  
793 =back
794  
795 =cut
796  
797 # ================================================================
798  
799 =head1 EXAMPLES
800  
801 Typical construction examples:
802  
803 my $o_rw = File::Data->new($filename, 'rw');
804  
805 my $o_ro = File::Data->new($filename, 'ro');
806  
807 =over 4
808  
809 =item complete
810  
811 my $o_dat = File::Data->new('./jabber');
812  
813 $o_dat->write(" Bewxre the Jabberwock my son,\n");
814  
815 $o_dat->prepend("The Jxbberwock by Lewis Cxrroll:\n");
816  
817 $o_dat->append(" the claws thxt snxtch,\n ...\n");
818  
819 $o_dat->insert(2, " the jaws which bite.\n");
820  
821 $o_dat->replace('x', 'a');
822  
823 print $o_dat->SEARCH('The.+\n')->REPLACE("The.+\n", '')->return('search');
824  
825 print $o_dat->READ;
826  
827 =item error
828  
829 Failure is indicated by an error routine being called, this will print
830 out any error to STDERR, unless warnings are declared fatal, in which
831 case we croak. You can register your own error handlers for any method
832 mentioned in the L<METHOD> section of this document, in addition is a
833 special B<init> call for initial file opening and general setting up.
834  
835 Create a read-write object with a callback for all errors:
836  
837 my $o_rw = File::Data->new($filename, 'ro', {
838 'error' => \&myerror,
839 });
840  
841 Create a read-only object with a separate object handler for each error type:
842  
843 my $o_rw = File::Data->new($filename, 'rw', {
844 'error' => $o_generic->error_handler,
845 'insert' => $o_handler->insert_error,
846 'open' => $o_open_handler,
847 'read' => \&carp,
848 'write' => \&write_error,
849 });
850  
851 =item commandline
852  
853 From the command line:
854  
855 C<perl -MFile::Data -e "File::Data->new('./test.txt')->write('some stuff')">
856  
857 And (very non-obfuscated)
858  
859 C<
860 perl -MFile::Data -e "@x=sort qw(perl another hacker just);
861 print map {split(\"\n\", ucfirst(\$_).\" \")}\
862 File::Data->new(\"./t/japh\")->\
863 write(shift(@x).\"\n\")-> \
864 append(shift(@x).\"\n\")-> \
865 prepend(shift(@x).\"\n\")-> \
866 insert(2, shift(@x).\"\n\")->\
867 READ;"
868 >
869  
870 If you still have problems, mail me the output of
871  
872 make test TEST_VERBOSE=1
873  
874 =cut
875  
876 # ================================================================
877  
878 # Private methods not expected to be called by anybody, and completely
879 # unsupported. Expected to metamorphose regularly - do B<not> call these - you
880 # have been warned!
881  
882 # Variable get/set method
883 #
884 # my $get = $o_dat->_var($key); # get
885 #
886 # my $set = $o_dat->_var($key, $val); # set
887  
888 # @_METHODS, qw(append insert prepend read replace return search write);
889 1 my $_VARS = join('|', @_METHODS, qw(
890 backup error errors filename filehandle last limbo permissions state writable
891 ));
892  
893 5964 ▶sub _var {
Subroutine _var: called CC=3 SCAR=11.1
894 5964 my $self = shift;
895 5964 my $key = shift;
896 5964 my $val = shift;
897 5964 my $ret = '';
898  
899 # if (!(grep(/^_$key$/, keys %{$self{'_var'}}))) {
900 5964 ▶ if ($key !~ /^($_VARS)$/io) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $key =~ /^($_VARS)$/io) { } 0 5964 5964
901 0 $self->_error("No such key($key) val($val)!");
902 } else {
903 5964 ▶ if (defined($val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 2259 3705 5964
904 2259 $self->{'_var'}{$key} = $val;
905 # {"$File::Data::$key"} = $val;
906 2259 $self->_debug("set key($key) => val($val)");
907 }
908 5964 $ret = $self->{'_var'}{$key};
909 }
910  
911 5964 return $ret;
912 }
913  
914 # Print given args on STDOUT
915 #
916 # $o_dat->_debug($msg) if $File::Data::DEBUG;
917  
918 2598 ▶sub _debug {
Subroutine _debug: called CC=5 SCAR=20.3
919 2598 my $self = shift;
920  
921 2598 my $state = $self->{'_var'}{'state'}; # ahem
922 2598 my $debug = $File::Data::DEBUG;
923  
924 2598 ▶ if (($debug =~ /^(\d+)$/o && $1 >= 1) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($debug =~ /^(\d+)$/o and $1 >= 1 or $debug =~ /^(.+)$/o and $state =~ /$debug/) 0 2598 2598
Condition33%3/9
$debug =~ /^(\d+)$/o and $1 >= 1 or $debug =~ /^(.+)$/o and $state =~ /$debug/
l!l&&r!l&&!r
002598
$debug =~ /^(\d+)$/o and $1 >= 1
!ll&&!rl&&r
025980
$debug =~ /^(.+)$/o and $state =~ /$debug/
!ll&&!rl&&r
025980
MC/DC0%0/4
$debug =~ /^(\d+)$/o and $1 >= 1 or $debug =~ /^(.+)$/o and $state =~ /$debug/
$debug =~ /^(\d+)$/o $1 >= 1 $debug =~ /^(.+)$/o $state =~ /$debug/
Truth table14%1/7
A and B or C and D
A= $debug =~ /^(\d+)$/oB= $1 >= 1C= $debug =~ /^(.+)$/oD= $state =~ /$debug/
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
925 $debug =~ /^(.+)$/o && $state =~ /$debug/
926 ) {
927 0 print ("$state: ", @_, "\n");
928 }
929  
930 2598 return ();
931 }
932  
933 # Return dumped env and object B<key> and B<values>
934 #
935 # print $o_dat->_vars;
936  
937 0 ▶sub _vars {
Subroutine _vars: not called CC=4 SCAR=25.0
938 0 my $self = shift;
939 0 my $h_ret = $self;
940  
941 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
942 0 foreach my $key (keys %{File::Data::}) {
943 0 ▶ next unless $key =~ /^[A-Z]+$/o;
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /^[A-Z]+$/o 0 0 0
944 0 ▶ next if $key =~ /^(BEGIN|EXPORT)/o;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /^(BEGIN|EXPORT)/o 0 0 0
945 0 my $var = "File::Data::$key";
946 0 $$h_ret{'_pck'}{$key} = $$var;
947 }
948  
949 0 return Dumper($h_ret);
950 }
951  
952 # Get/set error handling methods/objects
953 #
954 # my $c_sub = $o_dat->_err('insert'); # or default
955  
956 405 ▶sub _err {
Subroutine _err: called CC=3 SCAR=12.7
957 405 my $self = shift;
958 405 ▶ my $state = shift || $self->_var('state');
Condition33%1/3
shift() || $self->_var('state')
l!l&&r!l&&!r
40500
MC/DC0%0/2
shift() || $self->_var('state')
shift() $self->_var('state')
Truth table33%1/3
A || B
A= shift()B= $self->_var('state')
ABresult
1X1
011
000
959  
960 405 ▶ my $err = $self->{'_err'}{$state} || $self->{'_err'}{'default'};
Condition33%1/3
$self->{'_err'}{$state} || $self->{'_err'}{'default'}
l!l&&r!l&&!r
00405
MC/DC0%0/2
$self->{'_err'}{$state} || $self->{'_err'}{'default'}
$self->{'_err'}{$state} $self->{'_err'}{'default'}
Truth table33%1/3
A || B
A= $self->{'_err'}{$state}B= $self->{'_err'}{'default'}
ABresult
1X1
011
000
961  
962 405 return $err;
963 }
964  
965 # By default prints error to STDERR, will B<croak> if B<File::Data::FATAL> set,
966 # returning (). See L<EXAMPLES> for info on how to pass your own error
967 # handlers in.
968  
969 405 ▶sub _error {
Subroutine _error: called CC=10 SCAR=35.1
970 405 my $self = shift;
971 405 my @err = @_;
972 405 my @ret = ();
973  
974 405 my $state = $self->_var('state');
975 405 my $c_ref = $self->_err($state );
976 405 my $error = $self->_var('error');
977 405 unshift(@err, "$state ERROR: ");
978 405 my $ref = $self->_var('errors', join("\n", @err));
979  
980 # $self->_debug($self->_vars) if $File::Data::DEBUG;
981  
982 405 ▶ if (ref($c_ref) eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $c_ref eq 'CODE') { } 0 405 405
elsif (ref $c_ref and $c_ref->can($state)) { } 0 405 405
983 0 eval { @ret = &$c_ref(@err) };
984 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
985 0 ▶ $File::Data::FATAL >= 1
Branch: 1 branch
BranchTrueFalseTotal
$File::Data::FATAL >= 1 ? : 0 0 0
986 ? croak("$0 failed: $c_ref(@err)")
987 : carp("$0 failed: $c_ref(@err)")
988 ;
989 }
990 ▶ } elsif (ref($c_ref) && $c_ref->can($state)) {
Condition33%1/3
ref $c_ref and $c_ref->can($state)
!ll&&!rl&&r
40500
MC/DC0%0/2
ref $c_ref and $c_ref->can($state)
ref $c_ref $c_ref->can($state)
Truth table33%1/3
A and B
A= ref $c_refB= $c_ref->can($state)
ABresult
0X0
100
111
991 0 eval { @ret = $c_ref->$state(@err) };
992 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
993 0 ▶ $File::Data::FATAL >= 1
Branch: 1 branch
BranchTrueFalseTotal
$File::Data::FATAL >= 1 ? : 0 0 0
994 ? croak("$0 failed: $c_ref(@err)")
995 : carp("$0 failed: $c_ref(@err)")
996 ;
997 }
998 } else {
999 405 ▶ unless ($File::Data::SILENT) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($File::Data::SILENT) 0 405 405
1000 0 ▶ ($File::Data::FATAL >= 1) ? croak(@err) : carp(@err);
Branch: 1 branch
BranchTrueFalseTotal
$File::Data::FATAL >= 1 ? : 0 0 0
1001 }
1002 }
1003  
1004 405 return (); #
1005 }
1006  
1007 # my $file = $o_dat->_mapfile($filename);
1008  
1009 556 ▶sub _mapfile {
Subroutine _mapfile: called CC=5 SCAR=16.5
1010 556 my $self = shift;
1011 556 ▶ my $file = shift || '';
Condition100%2/2
shift() || ''
l!l
5542
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1012  
1013 556 $file =~ s/^\s*//o;
1014 556 $file =~ s/\s*$//o;
1015  
1016 556 ▶ unless ($file =~ /\w+/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /\w+/o) { } 552 4 556
1017 4 $file = '';
1018 4 $self->_error("inappropriate filename($file)");
1019 } else {
1020 552 ▶ my $xfile = $self->_var('filename') || '';
Condition50%1/2
$self->_var('filename') || ''
l!l
0552
MC/DC0%0/1
$self->_var('filename') || ''
$self->_var('filename')
Truth table50%1/2
A
A= $self->_var('filename')
Aresult
11
00
1021 552 ▶ if ($xfile =~ /.+/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xfile =~ /.+/o) 0 552 552
1022 0 $file = '';
1023 0 $self->_error("can't reuse ".ref($self)." object($xfile) for another file($file)");
1024 }
1025 }
1026  
1027 556 return $file;
1028 }
1029  
1030 # Maps given permissions to appropriate form for B<FileHandle>
1031 #
1032 # my $perms = $o_dat->_mapperms('+<');
1033  
1034 552 ▶sub _mapperms {
Subroutine _mapperms: called CC=4 SCAR=14.2
1035 552 my $self = shift;
1036 552 ▶ my $args = shift || '';
Condition50%1/2
shift() || ''
l!l
5520
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1037  
1038 552 $args =~ s/^\s*//o;
1039 552 $args =~ s/\s*$//o;
1040  
1041 552 my %map = ( # we only recognise
1042 'ro' => '<',
1043 'readonly' => '<',
1044 'rw' => '+<',
1045 'readwrite' => '+<',
1046 );
1047 552 ▶ my $ret = $map{$args} || $args;
Condition66%2/3
$map{$args} || $args
l!l&&r!l&&!r
65460
MC/DC0%0/2
$map{$args} || $args
$map{$args} $args
Truth table66%2/3
A || B
A= $map{$args}B= $args
ABresult
1X1
011
000
1048  
1049 552 ▶ $self->_error("Inappropriate permissions($args) - use this: ".Dumper(\%map))
Branch: 1 branch
BranchTrueFalseTotal
unless $ret =~ /.+/o 0 552 552
1050 unless $ret =~ /.+/o;
1051  
1052 552 return $ret;
1053 }
1054  
1055 # Map error handlers, if given
1056 #
1057 # my $h_errs = $o_dat->_maperrs(\%error_handlers);
1058  
1059 552 ▶sub _mapperrs {
Subroutine _mapperrs: called CC=4 SCAR=15.2
1060 552 my $self = shift;
1061 552 ▶ my $h_errs = shift || {};
Condition50%1/2
shift() || {}
l!l
5520
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1062  
1063 552 ▶ if (ref($h_errs) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $h_errs ne 'HASH') { } 0 552 552
1064 0 $self->_error("invalid error_handlers($h_errs)");
1065 } else {
1066 552 foreach my $key (%{$h_errs}) {
1067 0 $self->{'_err'}{$key} = $$h_errs{$key};
1068 }
1069 }
1070  
1071 552 return $self->{'_err'};
1072 }
1073  
1074 # Mark the entering of a special section, or state
1075 #
1076 # my $entered = $o_dat->enter('search');
1077  
1078 27 ▶sub _enter {
Subroutine _enter: called CC=2 SCAR=6.9
1079 27 my $self = shift;
1080 27 my $sect = shift;
1081  
1082 27 my $last = $self->_var('state');
1083 27 ▶ $self->_var('last' => $last) unless $last eq 'limbo';
Branch: 1 branch
BranchTrueFalseTotal
unless $last eq "limbo" 6 21 27
1084 27 my $next = $self->_var('state' => $sect);
1085  
1086 # $self->_debug("vars") if $File::Data::DEBUG;
1087  
1088 27 return $next;
1089 }
1090  
1091 # Mark the leaving of a special section, or state
1092 #
1093 # my $left = $o_dat->_leave('search');
1094  
1095 583 ▶sub _leave {
Subroutine _leave: called CC=2 SCAR=6.9
1096 583 my $self = shift;
1097 583 my $sect = shift;
1098  
1099 583 my $last = $self->_var('state');
1100 583 ▶ $self->_var('last' => $last) unless $last eq 'limbo';
Branch: 1 branch
BranchTrueFalseTotal
unless $last eq "limbo" 577 6 583
1101 583 my $next = $self->_var('state' => 'limbo');
1102  
1103 # $self->_debug("leaving state($last) => next($next)") if $File::Data::DEBUG;
1104  
1105 583 return $last;
1106 }
1107  
1108 # Get and set B<FileHandle>. Returns undef otherwise.
1109 #
1110 # my $FH = $o_dat->_fh($FH);
1111  
1112 490 ▶sub _fh {
Subroutine _fh: called CC=3 SCAR=11.0
1113 490 my $self = shift;
1114 490 my $arg = shift;
1115  
1116 490 ▶ my $FH = (defined($arg)
Branch: 1 branch
BranchTrueFalseTotal
defined $arg ? : 154 336 490
1117 ? $self->_var('filehandle', $arg)
1118 : $self->_var('filehandle')
1119 );
1120 490 ▶ $self->_error("no filehandle($FH)") unless $FH;
Branch: 1 branch
BranchTrueFalseTotal
unless $FH 0 490 490
1121  
1122 490 return $FH;
1123 }
1124  
1125 # ================================================================
1126 # Return values:
1127 #
1128 # 1 = success
1129 #
1130 # 0 = failure
1131  
1132 # Setup object, open a file, with permissions.
1133 #
1134 # my $i_ok = $o_dat->_init( $file, $perm, $h_errs );
1135  
1136 556 ▶sub _init {
Subroutine _init: called CC=9 SCAR=22.1
1137 556 my $self = shift;
1138 556 my $file = shift;
1139 556 my $perm = shift;
1140 556 my $h_err= shift;
1141 556 my $i_ok = 0;
1142  
1143 # $self->_enter('init');
1144 556 ▶ $self->_debug("in: file($file), perm($perm), h_err($h_err)") if $File::Data::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Data::DEBUG 0 556 556
1145  
1146 556 $file = $self->_mapfile($file );
1147 556 ▶ $perm = $self->_mapperms($perm ) if $file;
Branch: 1 branch
BranchTrueFalseTotal
if $file 552 4 556
1148 556 ▶ $h_err = $self->_mapperrs($h_err) if $file; # if $perm
Branch: 1 branch
BranchTrueFalseTotal
if $file 552 4 556
1149  
1150 556 ▶ if ($file) { # unless $h_err
Branch: 1 branch
BranchTrueFalseTotal
if ($file) 552 4 556
1151 552 $i_ok = $self->_check_access($file, $perm);
1152 552 ▶ if ($i_ok == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i_ok == 1) 167 385 552
1153 167 $file = $self->_var('filename', $file);
1154 167 $perm = $self->_var('permissions', $perm);
1155 167 $i_ok = $self->_open($file, $perm);
1156 167 ▶ $i_ok = $self->_backup() if $i_ok && $self->_var('backup');
Branch: 1 branch
BranchTrueFalseTotal
if $i_ok and $self->_var("backup") 0 167 167
Condition66%2/3
$i_ok and $self->_var("backup")
!ll&&!rl&&r
151520
MC/DC0%0/2
$i_ok and $self->_var("backup")
$i_ok $self->_var("backup")
Truth table66%2/3
A and B
A= $i_okB= $self->_var("backup")
ABresult
0X0
100
111
1157 }
1158 }
1159 # $self->_error("failed for file($file) and perm($perm)") unless $i_ok == 1;
1160  
1161 556 ▶ $self->_debug("out: $i_ok") if $File::Data::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Data::DEBUG 0 556 556
1162 556 $self->_leave('init');
1163  
1164 556 return $i_ok;
1165 }
1166  
1167 # Checks the args for existence and appropriate permissions etc.
1168 #
1169 # my $i_isok = $o_dat->_check_access($filename, $permissions);
1170  
1171 552 ▶sub _check_access {
Subroutine _check_access: called CC=8 SCAR=21.2
1172 552 my $self = shift;
1173 552 my $file = shift;
1174 552 my $perm = shift;
1175 552 my $i_ok = 0;
1176  
1177 552 ▶ if (!($file =~ /.+/o && $perm =~ /.+/o)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $file =~ /.+/o && $perm =~ /.+/o) { } 0 552 552
Condition33%1/3
$file =~ /.+/o && $perm =~ /.+/o
!ll&&!rl&&r
00552
MC/DC0%0/2
$file =~ /.+/o && $perm =~ /.+/o
$file =~ /.+/o $perm =~ /.+/o
Truth table33%1/3
A && B
A= $file =~ /.+/oB= $perm =~ /.+/o
ABresult
0X0
100
111
1178 0 $self->_error("no filename($file) or permissions($perm) given!");
1179 } else {
1180 552 stat($file); # just once
1181 552 ▶ if (! -e _) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -e _) { } 0 552 552
1182 0 $self->_error("target($file) does not exist!");
1183 } else {
1184 552 ▶ if (! -f _) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -f _) { } 1 551 552
1185 1 $self->_error("target($file) is not a file!");
1186 } else {
1187 551 ▶ if (!-r _) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -r _) { } 256 295 551
1188 256 $self->_error("file($file) cannot be read by effective uid($>) or gid($))!");
1189 } else {
1190 295 ▶ if ($perm =~ /^<$/o) { # readable
Branch: 1 branch
BranchTrueFalseTotal
if ($perm =~ /^<$/o) { } 6 289 295
1191 6 $i_ok++;
1192 } else {
1193 289 ▶ if (! -w $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -w $file) { } 128 161 289
1194 128 $self->_error("file($file) cannot be written by effective uid($>) or gid($))!");
1195 } else { # writable
1196 161 $self->_var('writable' => 1);
1197 161 $i_ok++;
1198 }
1199 }
1200 }
1201 }
1202 }
1203 }
1204  
1205 552 return $i_ok;
1206 }
1207  
1208 # Open the file
1209 #
1210 # my $i_ok = $o_dat->_open;
1211  
1212 167 ▶sub _open {
Subroutine _open: called CC=4 SCAR=13.9
1213 167 my $self = shift;
1214 167 my $file = $self->_var('filename');
1215 167 my $perm = $self->_var('permissions');
1216 167 my $i_ok = 0;
1217  
1218 167 my $open = "$perm $file";
1219 167 $self->_debug("using open($open)");
1220  
1221 167 ▶ my $FH = FileHandle->new("$perm $file") || '';
Condition100%2/2
'FileHandle'->new("$perm $file") || ''
l!l
15413
MC/DC100%1/1
'FileHandle'->new("$perm $file") || ''
'FileHandle'->new("$perm $file")
Truth table100%2/2
A
A= 'FileHandle'->new("$perm $file")
Aresult
11
00
1222 167 my @file = ();
1223 # my $FH = tie(@file, 'Tie::File', $file) or '';
1224 167 ▶ if (!$FH) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $FH) { } 13 154 167
1225 13 $self->_error("Can't get handle($FH) for file($file) with permissions($perm)! $!");
1226 } else {
1227 # $FH = $self->_fh(\@file);
1228 154 $FH = $self->_fh($FH);
1229 154 ▶ if ($FH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($FH) 154 0 154
1230 154 $i_ok++;
1231 154 $i_ok = $self->_lock(); # if $self->_writable;
1232 }
1233 154 $self->_debug("FH($FH) => i_ok($i_ok)");
1234 }
1235  
1236 167 return $i_ok;
1237 };
1238  
1239 # Lock the file
1240 #
1241 # my $i_ok = $o_dat->_lock;
1242  
1243 154 ▶sub _lock {
Subroutine _lock: called CC=5 SCAR=16.2
1244 154 my $self = shift;
1245 154 my $FH = $self->_fh;
1246 154 my $i_ok = 0;
1247  
1248 154 ▶ if ($FH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($FH) 154 0 154
1249 154 my $file = $self->_var('filename');
1250 154 ▶ if ($self->_writable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_writable) { } 148 6 154
1251 # if ($FH->flock(LOCK_EX | LOCK_NB)) {
1252 148 ▶ if (flock($FH, LOCK_EX | LOCK_NB)) {
Branch: 1 branch
BranchTrueFalseTotal
if (flock $FH, 6) { } 146 2 148
1253 146 $i_ok++;
1254 } else {
1255 2 $self->_error("Can't overlock file($file) handle($FH)!");
1256 }
1257 } else {
1258 # if ($FH->flock(LOCK_SH | LOCK_NB)) {
1259 6 ▶ if (flock($FH, LOCK_SH | LOCK_NB)) {
Branch: 1 branch
BranchTrueFalseTotal
if (flock $FH, 5) { } 6 0 6
1260 6 $i_ok++;
1261 } else {
1262 0 $self->_error("Can't lock shared file($file) handle($FH)!");
1263 }
1264 }
1265 }
1266  
1267 154 return $i_ok;
1268 };
1269  
1270 # Unlock the file
1271 #
1272 # my $i_ok = $o_dat->_unlock;
1273  
1274 0 ▶sub _unlock {
Subroutine _unlock: not called CC=2 SCAR=17.9
1275 0 my $self = shift;
1276 0 my $FH = $self->_fh;
1277 0 my $i_ok = 0;
1278  
1279 0 ▶ if ($FH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($FH) { } 0 0 0
1280 # if (flock($FH, LOCK_UN)) { apparently there's a race, perl does it better - see close :) }
1281 0 $i_ok++;
1282 } else {
1283 0 my $file = $self->_var('filename');
1284 0 $self->_error("Can't unlock file($file) handle($FH)!");
1285 }
1286  
1287 0 return $i_ok;
1288 }
1289  
1290 # Close the filehandle
1291 #
1292 # my $i_ok = $o_dat->_close;
1293  
1294 556 ▶sub _close {
Subroutine _close: called CC=4 SCAR=14.5
1295 556 my $self = shift;
1296 556 ▶ my $FH = $self->_fh if $self->_var('filehandle');
Branch: 1 branch
BranchTrueFalseTotal
if $self->_var("filehandle") 154 402 556
1297 556 my $i_ok = 0;
1298  
1299 556 ▶ if ($FH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($FH) 154 402 556
1300 # $FH->untie;
1301 154 ▶ if ($FH->close) { # perl unlocks it better than we can (race)
Branch: 1 branch
BranchTrueFalseTotal
if ($FH->close) { } 154 0 154
1302 154 $i_ok++;
1303 } else {
1304 0 $DB::single=2; #
1305 0 my $file = $self->_var('filename');
1306 0 $self->_error("Can't close file($file) handle($FH)!");
1307 }
1308 }
1309  
1310 556 return $i_ok;
1311 }
1312  
1313 170 ▶sub _writable {
Subroutine _writable: called CC=2 SCAR=6.9
1314 170 my $self = shift;
1315  
1316 170 my $i_ok = $self->_var('writable');
1317  
1318 170 ▶ if ($i_ok != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i_ok != 1) 10 160 170
1319 10 my $file = $self->_var('filename');
1320 10 my $perms = $self->_var('permissions');
1321 10 $self->_debug("$file not writable($i_ok) with permissions($perms)");
1322 }
1323  
1324 170 return $i_ok;
1325 }
1326  
1327 =item do
1328  
1329 Simple wrapper for method calls, returning the content.
1330  
1331 my @inserted = $o_dat->do('insert', @this);
1332  
1333 my @appended = $o_dat->do('append', @this);
1334  
1335 An addendum to this method, and to make life generally easier, is that
1336 you can also call any of the above methods in uppercase, to call via
1337 B<do()> eg;
1338  
1339 my @data = $o_dat->WRITE($this)->APPEND->($that)->read;
1340  
1341 First argument is the method to call, followed by the arguments that
1342 method expects.
1343  
1344 perl -MFile::Data -e "print File::Data->new($file)->INSERT(3,
1345 \"third line\n\")->READ";
1346  
1347 If you want to get at the output of a particular called method see
1348 L<return()>
1349  
1350 =cut
1351  
1352 6 ▶sub DO {
Subroutine DO: called CC=5 SCAR=16.7
Pod undocumented
1353 6 my $self = shift;
1354 6 my $call = shift;
1355 6 my @res = ();
1356  
1357 6 $self->_enter('do');
1358 6 ▶ $self->_debug('in: '.Dumper([$call, @_])) if $File::Data::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Data::DEBUG 0 6 6
1359  
1360 6 ▶ if ($call !~ /^($_METHODS)$/io) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $call =~ /^($_METHODS)$/io) { } 0 6 6
1361 0 $self->_error("unsupported method($call)");
1362 } else {
1363 6 $call = uc($call);
1364 6 $self->_var($call => []);
1365 6 my @res = $self->$call(@_);
1366 6 ▶ $self->_var($call => (ref($res[0]) ? $res[0] : \@res));
Branch: 1 branch
BranchTrueFalseTotal
ref $res[0] ? : 0 6 6
1367 }
1368  
1369 6 ▶ $self->_debug('out: $self') if $File::Data::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Data::DEBUG 0 6 6
1370 6 $self->_leave('do');
1371  
1372 6 return @res;
1373 }
1374  
1375 6 ▶sub do {
Subroutine do: called CC=1 SCAR=0.0
Pod documented
1376 6 my $self = shift;
1377  
1378 6 $self->DO(@_);
1379  
1380 6 return $self;
1381 }
1382  
1383 =back
1384  
1385 =cut
1386  
1387 # ================================================================
1388  
1389 556 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
1390 556 my $self = shift;
1391 556 $self->_close;
1392 }
1393  
1394 =head1 AUTHOR
1395  
1396 Richard Foley <File.Data@rfi.net>
1397  
1398 =head1 COPYRIGHT AND LICENSE
1399  
1400 Copyright (C) 2016 by Richard Foley
1401  
1402 This is free software; you can redistribute it and/or modify it under the same
1403 terms as the Perl 5 programming language system itself.
1404  
1405 =cut
1406  
1407 1 1;
1408  
↑ Summary