lib/Tie/File/FixedRecLen.pm

filter: Statementstmt 94.6% 107 / 113 Branchbran 71.2% 47 / 66 Conditioncond 36.3% 4 / 11 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 93.7% 15 / 16 Podpod n/a totaltotal 81.9% 173 / 211 CC 40 SCAR 19.7
CC
40
Cov
83%
TIEARRAY
12.3
_del_padding
11.7
_add_padding
9.5
CRAP 7.1
↑ Summary blib/lib/Tie/File/FixedRecLen/Store.pm »
1 package Tie::File::FixedRecLen;
2 {
3 37 $Tie::File::FixedRecLen::VERSION = '2.112531';
4 }
5  
6 37 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 37 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 37 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 37 ▶use base 'Tie::File';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 # v.0.97 says: "This version promises absolutely nothing about the internals,
13 # which may change without notice. A future version of the module will have a
14 # well-defined and stable subclassing API."
15  
16 37 ▶my $DEBUG = $ENV{FIXEDRECLEN_DEBUG} || 0;
Condition50%1/2
$ENV{'FIXEDRECLEN_DEBUG'} || 0
l!l
037
MC/DC0%0/1
$ENV{'FIXEDRECLEN_DEBUG'} || 0
$ENV{'FIXEDRECLEN_DEBUG'}
Truth table50%1/2
A
A= $ENV{'FIXEDRECLEN_DEBUG'}
Aresult
11
00
17 37 my @good_opts = qw(record_length pad_dir pad_char);
18  
19 # ===========================================================================
20  
21 2976 ▶sub TIEARRAY {
Subroutine TIEARRAY: called CC=10 SCAR=25.1
22 2976 my ($class, $file, %opts) = @_;
23 2976 my %tmp_opts = (); # ugh, Tie::File is broken for subclassing
24  
25 2976 for (@good_opts) {
26 8928 ▶ $tmp_opts{$_} = delete $opts{$_} if exists $opts{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{$_} 5948 2980 8928
27 }
28 2976 my $self = $class->SUPER::TIEARRAY(
29 $file, %opts, autodefer => 0, memory => 0,
30 );
31 2975 for (keys %tmp_opts) {
32 5948 $self->{$_} = $tmp_opts{$_};
33 }
34  
35 die "Useless use of Tie::File::FixedRecLen without a record_length\n"
36 if !exists $self->{record_length}
37 or !defined $self->{record_length}
38 or $self->{record_length} !~ m/^\d+$/
39 2975 ▶ or $self->{record_length} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if not exists $self->{"record_length"} && defined $self->{"record_length"} && $self->{"record_length"} =~ /^\d+$/ or $self->{"record_length"} == 0 0 2975 2975
Condition33%3/9
not exists $self->{"record_length"} && defined $self->{"record_length"} && $self->{"record_length"} =~ /^\d+$/ or $self->{"record_length"} == 0
l!l&&r!l&&!r
002975
exists $self->{'record_length'} && defined $self->{'record_length'} && $self->{'record_length'} =~ /^\d+$/
!ll&&!rl&&r
002975
exists $self->{"record_length"} and defined $self->{"record_length"}
!ll&&!rl&&r
002975
MC/DC0%0/4
not exists $self->{"record_length"} && defined $self->{"record_length"} && $self->{"record_length"} =~ /^\d+$/ or $self->{"record_length"} == 0
exists $self->{"record_length"} defined $self->{"record_length"} $self->{'record_length'} =~ /^\d+$/ $self->{"record_length"} == 0
Truth table20%1/5
not(A and B && C) or D
A= exists $self->{"record_length"}B= defined $self->{"record_length"}C= $self->{'record_length'} =~ /^\d+$/D= $self->{"record_length"} == 0
ABCDresult
0XXX1
10XX1
110X1
11111
11100
40  
41 2975 ▶ $self->{pad_dir} = 'left' if !defined $self->{pad_dir};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"pad_dir"} 2975 0 2975
42 2975 ▶ $self->{pad_char} = ' ' if !defined $self->{pad_char};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"pad_char"} 2 2973 2975
43 2975 return $self;
44 }
45  
46 # ===========================================================================
47 # utility methods
48  
49 548 ▶sub _add_padding {
Subroutine _add_padding: called CC=9 SCAR=22.5
50 548 my ($self, $rec) = @_;
51 548 ▶ $rec = '' if !defined $rec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rec 5 543 548
52 548 ▶ print STDERR "_add_padding(1) :$rec:\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 548 548
53  
54 # deferred records may have already been terminated with recsep
55 548 my $has_recsep = 0;
56 548 ▶ while (index($rec, $self->{recsep}, (length($rec) - $self->{recseplen})) != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($rec, $self->{"recsep"}, length($rec) - $self->{"recseplen"}) != -1) 8 548 556
57 8 substr($rec, - $self->{recseplen}) = ''; # temporary chomp
58 8 $has_recsep = 1;
59 }
60  
61 548 my $rl = length($rec);
62 die "Record '$rec' ($rl) exceeds record length ($self->{record_length})\n"
63 548 ▶ if $rl > $self->{record_length};
Branch: 1 branch
BranchTrueFalseTotal
if $rl > $self->{"record_length"} 0 548 548
64  
65 548 ▶ if (length($rec) != $self->{record_length}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $rec != $self->{"record_length"}) 548 0 548
66 my $pad
67 548 = $self->{pad_char} x ($self->{record_length} - length($rec));
68  
69 548 ▶ if ($self->{pad_dir} eq 'right') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'pad_dir'} eq 'right') { } 0 548 548
70 0 $rec .= $pad;
71 }
72 else {
73 548 $rec = $pad . $rec;
74 }
75 }
76  
77 548 ▶ $rec .= $self->{recsep} if $has_recsep;
Branch: 1 branch
BranchTrueFalseTotal
if $has_recsep 6 542 548
78 548 ▶ print STDERR "_add_padding(2) :$rec:\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 548 548
79  
80 548 return $rec;
81 }
82  
83 656 ▶sub _del_padding {
Subroutine _del_padding: called CC=10 SCAR=24.6
84 656 my ($self, $rec) = @_;
85 656 ▶ return undef if !defined $rec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rec 0 656 656
86 656 ▶ print STDERR "_del_padding(1) :$rec:\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 656 656
87  
88 656 my $has_recsep = 0;
89 656 ▶ while (index($rec, $self->{recsep}, (length($rec) - $self->{recseplen})) != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($rec, $self->{"recsep"}, length($rec) - $self->{"recseplen"}) != -1) 428 656 1084
90 428 substr($rec, - $self->{recseplen}) = ''; # temporary chomp
91 428 $has_recsep = 1;
92 }
93  
94 656 my $rl = length($rec);
95 die "Record '$rec' ($rl) is not set length ($self->{record_length})\n"
96 656 ▶ if $rl != $self->{record_length};
Branch: 1 branch
BranchTrueFalseTotal
if $rl != $self->{"record_length"} 0 656 656
97  
98 656 ▶ if ($self->{pad_dir} eq 'right') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'pad_dir'} eq 'right') { } 0 656 656
99 0 ▶ while (index($rec,$self->{pad_char},(length($rec) - 1))
Branch: 1 branch
BranchTrueFalseTotal
if (index($rec, $self->{"pad_char"}, length($rec) - 1) == length($rec) - 1) 0 0 0
100 == (length($rec) - 1)) {
101 0 substr($rec, -1, 1) = '';
102 }
103 }
104 else {
105 656 ▶ while (index($rec,$self->{pad_char},0) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($rec, $self->{"pad_char"}, 0) == 0) 6344 656 7000
106 6344 substr($rec, 0, 1) = '';
107 }
108 }
109  
110 656 ▶ $rec .= $self->{recsep} if $has_recsep;
Branch: 1 branch
BranchTrueFalseTotal
if $has_recsep 428 228 656
111 656 ▶ print STDERR "_del_padding(2) :$rec:\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 656 656
112  
113 656 return $rec;
114 }
115  
116 # ===========================================================================
117  
118 383 ▶sub FETCH {
Subroutine FETCH: called CC=2 SCAR=6.9
119 383 my ($self, $n) = @_;
120  
121 383 my $rec = $self->SUPER::FETCH($n);
122 383 ▶ return undef if !defined $rec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rec 5 378 383
123  
124 378 return $self->_del_padding($rec);
125 }
126  
127 # SUPER->STORE will append record separator for us
128 222 ▶sub STORE {
Subroutine STORE: called CC=1 SCAR=0.0
129 222 my ($self, $n, $rec) = @_;
130 222 $rec = $self->_add_padding($rec);
131  
132 222 return $self->SUPER::STORE($n, $rec);
133 }
134  
135 # need to override this as it is called from STORESIZE with $self->{recsep}
136 # sadly it could be called from STORE as well but that can't be helped.
137 0 ▶sub _store_deferred {
Subroutine _store_deferred: not called CC=1 SCAR=6.9
138 0 my ($self, $n, $rec) = @_;
139 0 $rec = $self->_add_padding($rec);
140  
141 0 return $self->SUPER::_store_deferred($n, $rec);
142 }
143  
144 286 ▶sub SPLICE {
Subroutine SPLICE: called CC=3 SCAR=11.0
145 286 my ($self, $pos, $nrecs, @data) = @_;
146  
147 286 map {$_ = $self->_add_padding($_)} @data;
148 286 my @result = $self->SUPER::SPLICE($pos, $nrecs, @data);
149  
150 # Yes, the return value of 'splice' *is* actually this complicated
151 wantarray
152 282 ▶ ? map {$self->_del_padding($_)} @result
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 143 139 282
@result ? : 80 59 139
153 : @result ? $self->_del_padding($result[-1]) : undef;
154 }
155  
156 # to work around _extend_file_to being called in many circumstances
157 12 ▶sub STORESIZE {
Subroutine STORESIZE: called CC=3 SCAR=11.0
158 12 my ($self, $len) = @_;
159  
160 12 ▶ $len += ($len > $self->FETCHSIZE ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$len > $self->FETCHSIZE ? : 5 7 12
161 12 ▶ print STDERR "STORESIZE $len\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 12 12
162 12 return $self->SUPER::STORESIZE($len);
163 }
164  
165 21 ▶sub EXTEND {
Subroutine EXTEND: called CC=3 SCAR=11.0
166 21 my ($self, $len) = @_;
167  
168 21 ▶ $len += ($len > $self->FETCHSIZE ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$len > $self->FETCHSIZE ? : 20 1 21
169 21 ▶ print STDERR "EXTEND $len\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 21 21
170 21 return $self->SUPER::EXTEND($len);
171 }
172  
173 # ===========================================================================
174  
175 # okay, according to the Tie::File code comments, the offset table has one
176 # more entry than the total number of records. I assume this means the last
177 # offset table entry is the seek position of the record which is next to be
178 # written in the file (or put another way, the size of the file).
179  
180 811 ▶sub _fill_offsets {
Subroutine _fill_offsets: called CC=3 SCAR=11.0
181 811 my ($self) = @_;
182  
183 811 my $fh = $self->{fh};
184 811 my $size = -s $fh;
185 811 my $off = $self->{offsets};
186 811 my $totreclen = $self->{record_length} + $self->{recseplen};
187  
188 # for development
189 # seek $fh,0,0;
190 # my $lines = join '',<$fh>;
191 # print STDERR "OFFSETS(1) content is :$lines:\n" if $DEBUG;
192  
193 811 ▶ die "File ($size) does not appear to be using ".
Branch: 1 branch
BranchTrueFalseTotal
if $size % $totreclen != 0 2 809 811
194 "fixed length records ($totreclen)\n" if ($size % $totreclen) != 0;
195  
196 809 @$off = map {$_ * $totreclen} (0 .. ($size / $totreclen));
197 809 ▶ print STDERR "OFFSETS(2) offsets :@$off:\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 809 809
198  
199 809 $self->_seek(-1); # position after the end of the last record
200 809 $self->{eof} = 1;
201  
202 809 return $#{$off};
203 }
204  
205 # populate the offsets table up to the beginning of record $n
206 # return the offset of record $n
207 662 ▶sub _fill_offsets_to {
Subroutine _fill_offsets_to: called CC=2 SCAR=6.9
208 662 my ($self, $n) = @_;
209  
210 662 $self->_fill_offsets;
211  
212 662 my $off = $self->{offsets};
213 662 ▶ return undef if $n > $#{$off};
Branch: 1 branch
BranchTrueFalseTotal
if $n > $#{$off;} 21 641 662
214 641 return $off->[$n];
215 }
216  
217 # We have read to the end of the file and have the offsets table
218 # entirely populated. Now we need to write a new record beyond
219 # the end of the file. We prepare for this by writing
220 # empty records into the file up to the position we want
221 #
222 # assumes that the offsets table already contains the offset of record $n,
223 # if it exists, and extends to the end of the file if not.
224 65 ▶sub _extend_file_to {
Subroutine _extend_file_to: called CC=4 SCAR=14.0
225 65 my ($self, $n) = @_;
226  
227 65 my $record = $self->{pad_char} x $self->{record_length};
228 65 my $recs = $self->_fill_offsets;
229 # a bit safer to just refresh this now
230 # and also positions us at the end of the file
231 # and gives us a starting counter for writing records
232  
233 65 ▶ print STDERR "_extend_file_to $n (-2)...\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 65 65
234 65 for ($recs .. ($n - 2)) {
235 140 ▶ print STDERR
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 140 140
236 "_extend_file_to $_ writing record '$record$self->{recsep}'\n"
237 if $DEBUG;
238 140 $self->_write_record($record . $self->{recsep});
239 }
240 65 $self->_fill_offsets; # refresh offsets table
241  
242 65 return undef; # not sure what Tie::File's version wants to return
243 }
244  
245 37 1;
246  
247 # ABSTRACT: Fixed Length Record support for Tie:File
248  
249  
250 __END__
↑ Summary blib/lib/Tie/File/FixedRecLen/Store.pm »