blib/lib/Tie/File/FixedRecLen/Store.pm

filter: Statementstmt 98.7% 76 / 77 Branchbran 63.6% 14 / 22 Conditioncond 70.0% 7 / 10 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 92.3% 12 / 13 Podpod n/a totaltotal 87.4% 111 / 127 CC 16 SCAR 15.2
CC
16
Cov
89%
TIEARRAY
9.4
STORE
4.0
STORESIZE
3.1
CRAP 4.6
« lib/Tie/File/FixedRecLen.pm ↑ Summary
1 package Tie::File::FixedRecLen::Store;
2 {
3 1 $Tie::File::FixedRecLen::Store::VERSION = '2.112531';
4 }
5  
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Fcntl qw(:flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # ===========================================================================
15  
16 3 ▶sub TIEARRAY {
Subroutine TIEARRAY: called CC=9 SCAR=22.4
17 3 my $class = shift;
18 3 my $file = shift;
19 3 my %args = @_;
20  
21 3 ▶ if (! defined $file or ! defined $args{record_length}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $file && defined $args{"record_length"} or $args{"record_length"} =~ /\D/) 2 1 3
Condition83%5/6
not defined $file && defined $args{"record_length"} or $args{"record_length"} =~ /\D/
l!l&&r!l&&!r
201
defined $file && defined $args{'record_length'}
!ll&&!rl&&r
111
MC/DC66%2/3
not defined $file && defined $args{"record_length"} or $args{"record_length"} =~ /\D/
defined $file defined $args{'record_length'} $args{"record_length"} =~ /\D/
Truth table75%3/4
not(A && B) or C
A= defined $fileB= defined $args{'record_length'}C= $args{"record_length"} =~ /\D/
ABCresult
0XX1
10X1
1111
1100
22 or $args{record_length} =~ m/\D/) {
23 2 croak "usage: tie \@ARRAY, '" . __PACKAGE__
24 . "', record_length => \$reclen";
25 }
26  
27 1 ▶ my $pad_char = $args{pad_char} || ' ';
Condition50%1/2
$args{'pad_char'} || ' '
l!l
01
MC/DC0%0/1
$args{'pad_char'} || ' '
$args{'pad_char'}
Truth table50%1/2
A
A= $args{'pad_char'}
Aresult
11
00
28 1 ▶ my $recsep = $args{recsep} || "\n";
Condition50%1/2
$args{'recsep'} || "\n"
l!l
01
MC/DC0%0/1
$args{'recsep'} || "\n"
$args{'recsep'}
Truth table50%1/2
A
A= $args{'recsep'}
Aresult
11
00
29 1 my $reclen = $args{record_length};
30 1 my $elemlen = ($reclen + length $recsep);
31  
32 # open file for appending
33 1 ▶ open (my $fh, '>>', $file)
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">>", $file or croak("can't open filename '${file}': $!\n") 1 0 1
34 or croak "can't open filename '$file': $!\n";
35 1 ▶ flock ($fh, LOCK_EX)
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, 2 or croak("can't lock file '${file}': $!\n") 1 0 1
36 or croak "can't lock file '$file': $!\n";
37  
38 # re-seek in case somebody wrote before we got the lock
39 # and set other things up like buffering
40 1 select ((select ($fh), $| = 1)[0]);
41 1 seek ($fh, 0, 2);
42 1 my $filesize = tell $fh;
43  
44 # check this looks like a FixedRecLen file
45 1 ▶ croak "file size ($filesize) does not match element length ($elemlen)\n"
Branch: 1 branch
BranchTrueFalseTotal
if $filesize % $elemlen != 0 0 1 1
46 if (($filesize % $elemlen) != 0);
47 1 my $num_records = ($filesize / $elemlen);
48  
49 1 return bless {
50 filename => $file,
51 fh => $fh,
52 pad_char => $pad_char,
53 reclen => $reclen,
54 recsep => $recsep,
55 recseplen => length $recsep,
56 elemlen => $elemlen,
57 records => $num_records, # will change
58 filesize => $filesize, # will change
59 }, $class;
60 }
61  
62 4 ▶sub PUSH {
Subroutine PUSH: called CC=3 SCAR=11.0
63 4 my $self = shift;
64 4 my @list = @_;
65 4 my $fh = $self->{fh};
66  
67 croak "length of value is greater than record length\n"
68 4 ▶ if grep {length $_ > $self->{reclen}} @list;
Branch: 1 branch
BranchTrueFalseTotal
if grep {length $_ > $self->{"reclen"};} @list 1 3 4
69  
70 croak "value contains record separator\n"
71 3 ▶ if grep {m/$self->{recsep}/} @list;
Branch: 1 branch
BranchTrueFalseTotal
if grep {/$self->{"recsep"}/;} @list 0 3 3
72  
73 # pad out (note: could run out of RAM doing this)
74 @list = map {
75 3 ($self->{pad_char} x ($self->{reclen} - length $_)) . $_
76 } @list;
77  
78 3 my $value = join $self->{recsep}, @list;
79  
80 3 print $fh $value, $self->{recsep};
81 3 $self->{records} += scalar @list;
82 3 $self->{filesize} = $self->{records} * $self->{elemlen};
83  
84 3 return $self->{records};
85 }
86  
87 2 ▶sub STORE {
Subroutine STORE: called CC=4 SCAR=13.9
88 2 my $self = shift;
89 2 my ($index, $value) = @_;
90 2 my $fh = $self->{fh};
91  
92 croak "length of value is greater than record length\n"
93 2 ▶ if (length $value > $self->{reclen});
Branch: 1 branch
BranchTrueFalseTotal
if length $value > $self->{"reclen"} 1 1 2
94  
95 1 ▶ croak "value contains record separator\n"
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /$self->{"recsep"}/ 0 1 1
96 if ($value =~ m/$self->{recsep}/);
97  
98 # random stores are not allowed, but PUSHes beyond file end are
99 1 my $blanks = $index - $self->{records};
100 1 ▶ croak "can only append to array, please see Tie::File::FixedRecLen\n"
Branch: 1 branch
BranchTrueFalseTotal
if $blanks < 0 0 1 1
101 if $blanks < 0;
102  
103 1 $self->PUSH( (map {''} (1 .. $blanks)), $value );
104  
105 1 return undef; # just what should STORE return?
106 }
107  
108 1 ▶sub STORESIZE {
Subroutine STORESIZE: called CC=3 SCAR=11.2
109 1 my $self = shift;
110 1 my ($count) = @_;
111  
112 1 ▶ return undef if $count == $self->{records};
Branch: 1 branch
BranchTrueFalseTotal
if $count == $self->{"records"} 0 1 1
113  
114 croak "cannot shorten, please see Tie::File::FixedRecLen\n"
115 1 ▶ if $count < $self->{records};
Branch: 1 branch
BranchTrueFalseTotal
if $count < $self->{"records"} 0 1 1
116  
117 1 $self->PUSH( map {''} (1 .. ($count - $self->{records})) );
118  
119 1 return undef; # just what should STORESIZE return?
120 }
121  
122 6 ▶sub FETCHSIZE {
Subroutine FETCHSIZE: called CC=1 SCAR=0.0
123 6 return $_[0]->{records};
124 }
125  
126 1 ▶sub UNTIE {
Subroutine UNTIE: called CC=1 SCAR=0.0
127 1 my $self = shift;
128 1 my $fh = $self->{fh};
129  
130 1 flock ($fh, LOCK_UN);
131 1 close $fh;
132 }
133  
134 1 foreach my $meth (qw/SPLICE FETCH POP SHIFT UNSHIFT CLEAR DELETE EXISTS EXTEND/) {
135 0 ▶ *{Symbol::qualify_to_ref($meth)} = sub {croak "unsupported method: '$meth'"};
Subroutine __ANON__: not called CC=1 SCAR=0.4
136 }
137  
138 1 1;
« lib/Tie/File/FixedRecLen.pm ↑ Summary