blib/lib/Tie/CSV_File.pm

filter: Statementstmt 99.4% 174 / 175 Branchbran 85.7% 36 / 42 Conditioncond 65.2% 15 / 23 MC/DCmcdc 26.6% 4 / 15 Subroutinesub 100% 39 / 39 Podpod n/a totaltotal 91.1% 268 / 294 CC 32 SCAR 13.8
CC
32
Cov
94%
TIEARRAY
9.0
set_new_fields
8.0
columns
8.0
CRAP 4.0
↑ Summary
1 package Tie::CSV_File;
2  
3 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 12 require Exporter;
7  
8 12 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use Tie::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use Text::CSV_XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use Tie::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use Params::Validate qw/:all/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 12 our @ISA = qw(Exporter Tie::Array);
16  
17 12 our $VERSION = '0.25';
18  
19 # There's a common misspelling of sepArated (an E instead of A)
20 # That's why all csv file definitions are defined even with an E and an A
21 60 ▶sub __mispell($) {
Subroutine __mispell: called CC=1 SCAR=0.0
22 60 shift =~ /^(.*_SEP)A(RATED)/;
23 60 return "$1E$2";
24 }
25  
26 # Export all predefined file types
27 60 our @EXPORT = map {($_, __mispell $_)}
28 12 map {$_ . "_SEPARATED"}
29 qw/TAB COLON SEMICOLON PIPE WHITESPACE/;
30  
31 12 ▶use constant SPLIT_SEPARATED_STANDARD_OPTIONS => (
Subroutine BEGIN: called CC=1 SCAR=0.0
32 quote_char => undef,
33 eol => undef, # default
34 escape_char => undef,
35 always_quote => 0 # default
36 12 );
37  
38 12 ▶use constant SEPARATOR_CHARS => (
Subroutine BEGIN: called CC=1 SCAR=0.0
39 [TAB => "\t"],
40 [COLON => ":"],
41 [SEMICOLON => ";"],
42 [PIPE => "|"]
43 12 );
44  
45 # Create typical file format constants,
46 # only different on their seperator chars
47 12 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
48 12 foreach (SEPARATOR_CHARS) {
49 48 my ($name, $char) = @$_;
50 48 $name .= "_SEPARATED";
51 48 ▶ eval "use constant $name => (sep_char => \$char,
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
52 SPLIT_SEPARATED_STANDARD_OPTIONS)";
53 48 (my $name_with_spelling_mistake = $name) =~ s/(?<=SEP)A(?=RATED)/E/;
54 48 eval "*$name_with_spelling_mistake = *$name";
55 };
56 }
57 # Note that the BEGIN block is necessary for Perl <= 5.6.1
58 # otherwise it detects too late the constant creation
59 # and signalizes the *_SEPARATED as barewords :-((
60  
61 12 ▶use constant WHITESPACE_SEPARATED => (
Subroutine BEGIN: called CC=1 SCAR=0.0
62 sep_re => qr/\s+/,
63 sep_char => ' ',
64 quote_char => undef,
65 eol => undef, # default
66 escape_char => undef,
67 always_quote => 0 # default
68 12 );
69 12 *WHITESPACE_SEPERATED = *WHITESPACE_SEPARATED;
70 # ^ ^ you see the difference
71  
72 113 ▶sub TIEARRAY {
Subroutine TIEARRAY: called CC=9 SCAR=22.0
73 113 my ($class, $fname) = (shift(), shift());
74
75 # Parameter validation
76 113 my %options = validate( @_, {
77 quote_char => {default => q/"/, type => SCALAR | UNDEF},
78 eol => {default => undef, type => SCALAR | UNDEF},
79 sep_char => {default => q/,/, type => SCALAR | UNDEF},
80 sep_re => {default => undef, isa => 'Regexp'},
81 escape_char => {default => q/"/, type => SCALAR | UNDEF},
82 always_quote => {default => 0, type => SCALAR | UNDEF}
83 });
84
85 110 $options{binary} = 1; # to handle with 'ä','ö','ü' and so on, not for "\n"
86
87 # Check for some cases to warn
88 110 ▶ unless( defined $options{sep_char} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $options{"sep_char"}) 1 109 110
89 1 carp "The sep_char should either be defined or not mentioned, ".
90 "but I got something like sep_char => undef\n" .
91 "It's interpreted as the default value ',' (a comma)!";
92 1 $options{sep_char} = ',';
93 }
94 110 ▶ unless ( (my $l = length $options{sep_char}) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ((my $l = length $options{"sep_char"}) == 1) 3 107 110
95 3 carp "The sep_char should have a length of 1, not $l - reset it to default ','";
96 3 $options{sep_char} = ',';
97 }
98 110 ▶ if (defined(my $c = $options{sep_char}) && defined(my $r = $options{sep_re})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $c = $options{"sep_char"}) and defined(my $r = $options{"sep_re"})) 10 100 110
Condition66%2/3
defined(my $c = $options{"sep_char"}) and defined(my $r = $options{"sep_re"})
!ll&&!rl&&r
010010
MC/DC50%1/2
defined(my $c = $options{"sep_char"}) and defined(my $r = $options{"sep_re"})
defined(my $c = $options{"sep_char"}) defined(my $r = $options{"sep_re"})
Truth table66%2/3
A and B
A= defined(my $c = $options{"sep_char"})B= defined(my $r = $options{"sep_re"})
ABresult
0X0
100
111
99 10 ▶ carp "The sep_char '$c' is itself not matched by the sep_re '$r'"
Branch: 1 branch
BranchTrueFalseTotal
unless $c =~ /$r/ 2 8 10
100 if $c !~ /$r/;
101 }
102
103 110 ▶ tie my @lines, 'Tie::File', $fname or die "Can't open $fname: $!";
Branch: 1 branch
BranchTrueFalseTotal
tie my @lines, "Tie::File", $fname or die "Can't open ${fname}: $!" 109 1 110
104 # options are almost same for Text::CSV_XS
105 # but sep_re is unknown to Text::CSV_XS
106 # so remove it temporarely
107 109 my %csv_xs_options = %options;
108 109 delete $csv_xs_options{sep_re};
109 109 ▶ if (not defined($csv_xs_options{eol})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $csv_xs_options{"eol"}) 100 9 109
110 100 delete $csv_xs_options{eol};
111 }
112 109 my $csv_xs = Text::CSV_XS->new(\%csv_xs_options);
113 109 ▶ if (not defined($csv_xs)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $csv_xs) 0 109 109
114 0 die "Could not initialize Text::CSV_XS with options " . Dumper(\%csv_xs_options);
115 }
116 my $self = {
117 lines => \@lines,
118 csv => $csv_xs,
119 quote_char => $options{quote_char},
120 escape_char => $options{escape_char},
121 always_quote=> $options{always_quote},
122 109 };
123 109 $self->{sep_char} = $options{sep_char};
124 109 $self->{eol} = $options{eol};
125 109 $self->{sep_re} = $options{sep_re};
126 109 bless $self, $class;
127 }
128  
129 1310 ▶sub FETCHSIZE {
Subroutine FETCHSIZE: called CC=1 SCAR=0.0
130 1310 my ($self) = @_;
131 1310 return scalar( @{ $self->{lines} } );
132 }
133  
134 1203 ▶sub FETCH {
Subroutine FETCH: called CC=1 SCAR=0.0
135 1203 my ($self, $line_nr) = @_;
136 1203 my @csv_options = map {$self->{$_}} qw/csv eol sep_char sep_re quote_char/;
137 1203 tie my @fields, 'Tie::CSV_File::Line', $self->{lines}, $line_nr, @csv_options;
138 1203 return \@fields;
139 }
140  
141 22 ▶sub EXISTS {
Subroutine EXISTS: called CC=1 SCAR=0.0
142 22 my ($self, $line_nr) = @_;
143 22 exists $self->{lines}->[$line_nr];
144 }
145  
146 235 ▶sub STORE {
Subroutine STORE: called CC=4 SCAR=13.9
147 235 my ($self, $line_nr, $columns) = @_;
148 235 my $csv = $self->{csv};
149 235 ▶ if (@$columns) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$columns) { } 199 30 229
150 199 ▶ $csv->combine(@$columns) or die "Can't store " . Dumper($columns);
Branch: 1 branch
BranchTrueFalseTotal
$csv->combine(@$columns) or die q[Can't store ] . Dumper($columns) 199 0 199
151 199 $self->{lines}->[$line_nr] = $csv->string;
152 } else {
153 30 ▶ $self->{lines}->[$line_nr] = $self->{eol} || '';
Condition100%2/2
$self->{'eol'} || ''
l!l
129
MC/DC100%1/1
$self->{'eol'} || ''
$self->{'eol'}
Truth table100%2/2
A
A= $self->{'eol'}
Aresult
11
00
154 }
155 }
156  
157 5 ▶sub STORESIZE {
Subroutine STORESIZE: called CC=1 SCAR=0.0
158 5 my ($self, $count) = @_;
159 5 $#{$self->{lines}} = $count-1;
160 }
161  
162 6 ▶sub DELETE {
Subroutine DELETE: called CC=1 SCAR=0.0
163 6 my ($self, $line_nr) = @_;
164 6 delete $self->{lines}->[$line_nr];
165 }
166  
167 package Tie::CSV_File::Line;
168  
169 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
170 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
171  
172 12 ▶use Tie::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
173 12 ▶use Text::CSV_XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
174 12 ▶use Tie::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
175 12 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
176  
177 12 our @ISA = qw(Exporter Tie::Array);
178  
179 1203 ▶sub TIEARRAY {
Subroutine TIEARRAY: called CC=1 SCAR=0.0
180 1203 my ($class, $data, $line_nr, $csv, $eol, $sep_char, $sep_re, $quote_char) = @_;
181 1203 my $self = bless {
182 data => $data,
183 line_nr => $line_nr,
184 csv => $csv,
185 eol => $eol,
186 sep_char => $sep_char,
187 sep_re => $sep_re,
188 quote_char => $quote_char,
189 fields => undef
190 }, $class;
191 }
192  
193 1195 ▶sub columns {
Subroutine columns: called CC=8 SCAR=20.8
194 1195 my $self = shift;
195 1195 my @fields = (); # even if there aren't any fields, it's an empty list
196 1195 my $line = $self->{data}->[$self->{line_nr}];
197 1195 ▶ defined($line) or return $self->{fields} = \@fields;
Branch: 1 branch
BranchTrueFalseTotal
defined $line or return $self->{"fields"} = \@fields 1145 50 1195
198 1145 ▶ if (defined( my $eol = $self->{eol} )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $eol = $self->{'eol'})) { } 61 1084 1145
199 61 $line =~ s/\Q$eol\E$//;
200 } else {
201 1084 $line =~ s:$/$::; # remove default eol in $/ at the end
202 }
203 1145 ▶ if (length($line) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $line == 0) 194 951 1145
204 194 return $self->{fields} = []
205 };
206 951 ▶ if (defined( my $re = $self->{sep_re} )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $re = $self->{'sep_re'})) { } 50 901 951
207 push @fields,
208 50 ▶ map {defined($_) ? $_ : ''} # empty fields shall be '', not undef
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 182 0 182
209 grep !/$re/, # ugly, but needed see downside
210 split /($re)/, $line; # needed, as perl has problems with
211 # split /x/,"xxxxxxxxxx"; or similar
212 50 ▶ push @fields, '' if $line =~ /$re$/; # needed when the last element is empty
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /$re$/ 9 41 50
213 # - it won't be catched with split
214 } else {
215 901 my $csv = $self->{csv};
216 901 ▶ $csv->parse($line) and push @fields, $csv->fields();
Branch: 1 branch
BranchTrueFalseTotal
$csv->parse($line) and push @fields, $csv->fields 901 0 901
217 }
218 951 return $self->{fields} = \@fields;
219 }
220  
221 180 ▶sub set_new_fields {
Subroutine set_new_fields: called CC=8 SCAR=20.8
222 180 my ($self, $fields) = @_;
223 180 $self->{fields} = $fields;
224  
225 180 my $csv_string;
226 180 ▶ if (@$fields == 0) { # No columns
Branch: 2 branches
BranchTrueFalseTotal
if (@$fields == 0) { } 1 179 180
elsif (@$fields == 1 and $fields->[0] eq '') { } 21 158 179
227 1 my $eol = $self->{eol};
228 1 ▶ $csv_string = defined($eol) ? $eol : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $eol ? : 0 1 1
229 ▶ } elsif (@$fields == 1 and $fields->[0] eq '') { # One column with an empty string
Condition100%3/3
@$fields == 1 and $fields->[0] eq ""
!ll&&!rl&&r
1302821
MC/DC100%2/2
@$fields == 1 and $fields->[0] eq ""
@$fields == 1 $fields->[0] eq ""
Truth table100%3/3
A and B
A= @$fields == 1B= $fields->[0] eq ""
ABresult
0X0
100
111
230 21 my $quote_char = $self->{quote_char};
231 21 my $eol = $self->{eol};
232 $_ = defined($_) ? $_ : ""
233 21 ▶ for $eol, $quote_char;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 20 22 42
234  
235 21 $csv_string = $quote_char . $quote_char . $eol;
236 } else { # Default
237 158 my $csv = $self->{csv};
238 158 ▶ $csv->combine(@$fields) or die "Can't store columns " . Dumper($fields);
Branch: 1 branch
BranchTrueFalseTotal
$csv->combine(@$fields) or die q[Can't store columns ] . Dumper($fields) 158 0 158
239 158 $csv_string = $csv->string;
240 }
241 180 $self->{data}->[$self->{line_nr}] = $csv_string;
242 }
243  
244 4058 ▶sub FETCHSIZE {
Subroutine FETCHSIZE: called CC=2 SCAR=7.0
245 4058 my ($self) = @_;
246 4058 ▶ return scalar( @{$self->{fields} || $self->columns} );
Condition66%2/3
$self->{'fields'} || $self->columns
l!l&&r!l&&!r
31259330
MC/DC0%0/2
$self->{'fields'} || $self->columns
$self->{'fields'} $self->columns
Truth table66%2/3
A || B
A= $self->{'fields'}B= $self->columns
ABresult
1X1
011
000
247 }
248  
249 2881 ▶sub FETCH {
Subroutine FETCH: called CC=2 SCAR=7.1
250 2881 my ($self, $col_nr) = @_;
251 2881 ▶ ($self->{fields} || $self->columns)->[$col_nr];
Condition66%2/3
$self->{'fields'} || $self->columns
l!l&&r!l&&!r
2859220
MC/DC0%0/2
$self->{'fields'} || $self->columns
$self->{'fields'} $self->columns
Truth table66%2/3
A || B
A= $self->{'fields'}B= $self->columns
ABresult
1X1
011
000
252 }
253  
254 61 ▶sub EXISTS {
Subroutine EXISTS: called CC=2 SCAR=8.1
255 61 my ($self, $col_nr) = @_;
256 61 ▶ exists( ($self->{fields} || $self->columns)->[$col_nr] );
Condition33%1/3
$self->{'fields'} || $self->columns
l!l&&r!l&&!r
0610
MC/DC0%0/2
$self->{'fields'} || $self->columns
$self->{'fields'} $self->columns
Truth table33%1/3
A || B
A= $self->{'fields'}B= $self->columns
ABresult
1X1
011
000
257 }
258  
259 177 ▶sub STORE {
Subroutine STORE: called CC=2 SCAR=7.0
260 177 my ($self, $col_nr, $value) = @_;
261 177 my $csv = $self->{csv};
262 177 ▶ my $fields = $self->{fields} || $self->columns;
Condition66%2/3
$self->{'fields'} || $self->columns
l!l&&r!l&&!r
11760
MC/DC0%0/2
$self->{'fields'} || $self->columns
$self->{'fields'} $self->columns
Truth table66%2/3
A || B
A= $self->{'fields'}B= $self->columns
ABresult
1X1
011
000
263 177 $fields->[$col_nr] = $value;
264 177 $self->set_new_fields($fields);
265 }
266  
267 3 ▶sub STORESIZE {
Subroutine STORESIZE: called CC=2 SCAR=7.4
268 3 my ($self, $new_size) = @_;
269 3 ▶ my $fields = $self->{fields} || $self->columns;
Condition33%1/3
$self->{'fields'} || $self->columns
l!l&&r!l&&!r
030
MC/DC0%0/2
$self->{'fields'} || $self->columns
$self->{'fields'} $self->columns
Truth table33%1/3
A || B
A= $self->{'fields'}B= $self->columns
ABresult
1X1
011
000
270 3 $#$fields = $new_size-1; # Set new size => last element is now at
271 # index size-1
272 3 $self->set_new_fields($fields);
273 }
274  
275 14 ▶sub DELETE {
Subroutine DELETE: called CC=1 SCAR=0.0
276 14 my ($self, $col_nr) = @_;
277 14 $self->STORE($col_nr,"");
278 }
279  
280 12 1;
281 __END__
↑ Summary