lib/File/Data.pm

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