CSV.pm

filter: Statementstmt 88.1% 134 / 152 Branchbran 77.7% 28 / 36 Conditioncond 76.9% 10 / 13 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 87.5% 28 / 32 Podpod 50.0% 1 / 2 totaltotal 84.7% 206 / 243 CC 24 SCAR 15.3
CC
24
Cov
86%
parse_opts
12.0
TIEARRAY
7.2
_combine
3.5
CRAP 4.6
↑ Summary CSV/HoldRow.pm »
1 package Tie::Array::CSV;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 our $VERSION = '0.08';
7 7 $VERSION = eval $VERSION;
8  
9 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 7 ▶use Tie::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Text::CSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 7 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 7 ▶use Tie::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 our @ISA = ('Tie::Array');
18  
19 29 ▶sub parse_opts {
Subroutine parse_opts: called CC=12 SCAR=24.9
Pod undocumented
20 29 my $class = shift;
21  
22 29 ▶ croak "Must specify a file" unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 29 29
23  
24 29 my ($file, %opts);
25  
26 # handle one arg as either hashref (of opts) or file
27 29 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 10 19 29
28 10 ▶ if (ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 2 8 10
29 2 %opts = %{ shift() };
30 } else {
31 8 $file = shift;
32 }
33 }
34  
35 # handle file and hashref of opts
36 29 ▶ if (@_ == 2 and ref $_[1] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2 and ref $_[1] eq "HASH") 12 17 29
Condition100%3/3
@_ == 2 and ref $_[1] eq "HASH"
!ll&&!rl&&r
15212
MC/DC100%2/2
@_ == 2 and ref $_[1] eq "HASH"
@_ == 2 ref $_[1] eq "HASH"
Truth table100%3/3
A and B
A= @_ == 2B= ref $_[1] eq "HASH"
ABresult
0X0
100
111
37 12 $file = shift;
38 12 %opts = %{ shift() };
39 }
40  
41 # handle file before hash of opts
42 29 ▶ if (@_ % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2) 5 24 29
43 5 $file = shift;
44 }
45  
46 # handle hash of opts
47 29 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 7 22 29
48 7 %opts = @_;
49 }
50  
51 # handle file passed has hash(ref) value to 'file' key
52 29 ▶ if (!$file and defined $opts{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $file and defined $opts{"file"}) 4 25 29
Condition66%2/3
not $file and defined $opts{"file"}
!ll&&!rl&&r
2504
MC/DC50%1/2
not $file and defined $opts{"file"}
not $file defined $opts{"file"}
Truth table66%2/3
not(A) and B
A= not $fileB= defined $opts{"file"}
ABresult
0X0
100
111
53 4 $file = delete $opts{file};
54 }
55  
56 # file wasn't specified as lone arg or as a hash opt
57 29 ▶ croak "Must specify a file" unless $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 29 29
58  
59 # parse specific options
60 29 ▶ if (exists $opts{sep_char}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"sep_char"}) 4 25 29
61 4 $opts{text_csv}{sep_char} = delete $opts{sep_char};
62 }
63  
64 29 return ($file, \%opts);
65 }
66  
67 7 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
68 7 my $class = shift;
69 7 my ($file, $opts) = $class->parse_opts(@_);
70  
71 7 my @self;
72 7 tie @self, $class, $file, $opts;
73  
74 7 return \@self;
75  
76 }
77  
78 22 ▶sub TIEARRAY {
Subroutine TIEARRAY: called CC=7 SCAR=19.7
79 22 my $class = shift;
80 22 my ($file, $opts) = $class->parse_opts(@_);
81  
82 22 my @tiefile;
83 22 ▶ tie @tiefile, 'Tie::File', $file, recsep => "\n", %{ $opts->{tie_file} || {} }
Branch: 1 branch
BranchTrueFalseTotal
tie @tiefile, "Tie::File", $file, "recsep", "\n", %{$opts->{"tie_file"} or {};} or croak("Cannot tie file $file") 22 0 22
Condition50%1/2
$opts->{'tie_file'} || {}
l!l
022
MC/DC0%0/1
$opts->{'tie_file'} || {}
$opts->{'tie_file'}
Truth table50%1/2
A
A= $opts->{'tie_file'}
Aresult
11
00
84 or croak "Cannot tie file $file";
85  
86 22 my $csv;
87 22 ▶ if (blessed $opts->{text_csv} and $opts->{text_csv}->isa('Text::CSV')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($opts->{'text_csv'}) and $opts->{'text_csv'}->isa('Text::CSV')) { } 1 21 22
Condition66%2/3
builtin::blessed($opts->{"text_csv"}) and $opts->{"text_csv"}->isa("Text::CSV")
!ll&&!rl&&r
2101
MC/DC50%1/2
builtin::blessed($opts->{"text_csv"}) and $opts->{"text_csv"}->isa("Text::CSV")
builtin::blessed($opts->{"text_csv"}) $opts->{"text_csv"}->isa("Text::CSV")
Truth table66%2/3
A and B
A= builtin::blessed($opts->{"text_csv"})B= $opts->{"text_csv"}->isa("Text::CSV")
ABresult
0X0
100
111
88 1 $csv = $opts->{text_csv};
89 } else {
90 $csv = Text::CSV->new($opts->{text_csv} || {})
91 21 ▶ or croak "CSV (new) error: " . Text::CSV->error_diag();
Branch: 1 branch
BranchTrueFalseTotal
$csv = "Text::CSV"->new($opts->{"text_csv"} || {}) or croak('CSV (new) error: ' . "Text::CSV"->error_diag) 21 0 21
Condition100%2/2
$opts->{'text_csv'} || {}
l!l
813
MC/DC100%1/1
$opts->{'text_csv'} || {}
$opts->{'text_csv'}
Truth table100%2/2
A
A= $opts->{'text_csv'}
Aresult
11
00
92 }
93  
94 22 my $self = {
95 file => \@tiefile,
96 csv => $csv,
97 };
98  
99 22 bless $self, $class;
100  
101 22 return $self;
102 }
103  
104 92 ▶sub FETCH {
Subroutine FETCH: called CC=1 SCAR=0.0
105 92 my $self = shift;
106 92 my $index = shift;
107  
108 92 my $line = $self->{file}[$index];
109  
110 92 my $rowclass = ref($self) . '::Row';
111 tie my @line, $rowclass, {
112 file => $self->{file},
113 line_num => $index,
114 fields => $self->_parse($line),
115 csv => $self->{csv},
116 92 };
117  
118 92 return \@line;
119 }
120  
121 4 ▶sub STORE {
Subroutine STORE: called CC=1 SCAR=0.0
122 4 my $self = shift;
123 4 my ($index, $value) = @_;
124  
125 4 $self->{file}[$index] = $self->_combine($value);
126 }
127  
128 81 ▶sub FETCHSIZE {
Subroutine FETCHSIZE: called CC=1 SCAR=0.0
129 81 my $self = shift;
130  
131 81 return scalar @{ $self->{file} };
132 }
133  
134 3 ▶sub STORESIZE {
Subroutine STORESIZE: called CC=1 SCAR=0.0
135 3 my $self = shift;
136 3 my $new_size = shift;
137  
138 3 $#{ $self->{file} } = $new_size - 1;
139
140 }
141  
142 2 ▶sub EXISTS {
Subroutine EXISTS: called CC=1 SCAR=0.0
143 2 my $self = shift;
144 2 my ($index) = shift;
145 2 return exists $self->{file}[$index];
146 }
147  
148 1 ▶sub DELETE {
Subroutine DELETE: called CC=1 SCAR=0.0
149 1 my $self = shift;
150 1 my $index = shift;
151 1 return $self->SPLICE($index,1);
152 }
153  
154 93 ▶sub _parse {
Subroutine _parse: called CC=3 SCAR=11.3
155 93 my $self = shift;
156 93 my ($line) = @_;
157 93 ▶ $line = '' unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 3 90 93
158  
159 93 ▶ return [$self->{csv}->fields] if $self->{csv}->parse($line);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"csv"}->parse($line) 93 0 93
160  
161 0 croak "CSV parse error: " . $self->{csv}->error_diag;
162 }
163  
164 5 ▶sub _combine {
Subroutine _combine: called CC=3 SCAR=12.5
165 5 my $self = shift;
166 5 my ($value) = @_;
167  
168 return $self->{csv}->string
169 5 ▶ if $self->{csv}->combine( ref $value ? @$value : ($value) );
Branch: 2 branches
BranchTrueFalseTotal
if $self->{"csv"}->combine(ref $value ? @$value : $value) 5 0 5
ref $value ? : 5 0 5
170  
171 0 croak "CSV combine error: " . $self->{csv}->error_diag();
172 }
173  
174 package Tie::Array::CSV::Row;
175  
176 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
177  
178 7 ▶use Tie::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
179 7 our @ISA = ('Tie::Array');
180  
181 use overload
182 0 ▶ '@{}' => sub{ $_[0]{fields} };
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine __ANON__: not called CC=1 SCAR=0.2
183  
184 92 ▶sub TIEARRAY {
Subroutine TIEARRAY: called CC=1 SCAR=0.0
185 92 my $class = shift;
186 92 my $self = shift;
187  
188 92 bless $self, $class;
189  
190 92 return $self;
191 }
192  
193 195 ▶sub FETCH {
Subroutine FETCH: called CC=1 SCAR=0.0
194 195 my $self = shift;
195 195 my $index = shift;
196  
197 195 return $self->{fields}[$index];
198 }
199  
200 12 ▶sub STORE {
Subroutine STORE: called CC=1 SCAR=0.0
201 12 my $self = shift;
202 12 my ($index, $value) = @_;
203  
204 12 $self->{fields}[$index] = $value;
205  
206 12 $self->_update;
207  
208 }
209  
210 241 ▶sub FETCHSIZE {
Subroutine FETCHSIZE: called CC=1 SCAR=0.0
211 241 my $self = shift;
212  
213 241 return scalar @{ $self->{fields} };
214 }
215  
216 3 ▶sub STORESIZE {
Subroutine STORESIZE: called CC=1 SCAR=0.0
217 3 my $self = shift;
218 3 my $new_size = shift;
219  
220 my $return = (
221 3 $#{ $self->{fields} } = $new_size - 1
222 );
223  
224 3 $self->_update;
225  
226 3 return $return;
227 }
228  
229 1 ▶sub SHIFT {
Subroutine SHIFT: called CC=1 SCAR=0.0
230 1 my $self = shift;
231  
232 1 my $value = shift @{ $self->{fields} };
233  
234 1 $self->_update;
235  
236 1 return $value;
237 }
238  
239 0 ▶sub UNSHIFT {
Subroutine UNSHIFT: not called CC=1 SCAR=6.9
240 0 my $self = shift;
241 0 my $value = shift;
242  
243 0 unshift @{ $self->{fields} }, $value;
244  
245 0 $self->_update;
246  
247 0 return $self->FETCHSIZE();
248 }
249  
250 0 ▶sub DELETE {
Subroutine DELETE: not called CC=1 SCAR=6.9
251 0 my $self = shift;
252 0 my $index = shift;
253  
254 0 my $return = splice @{ $self->{fields} }, $index, 1;
255 0 $self->_update;
256  
257 0 return $return;
258 }
259  
260 0 ▶sub EXISTS {
Subroutine EXISTS: not called CC=1 SCAR=6.9
261 0 my $self = shift;
262 0 my $index = shift;
263  
264 0 return exists $self->{fields}[$index];
265 }
266  
267 16 ▶sub _update {
Subroutine _update: called CC=3 SCAR=11.0
268 16 my $self = shift;
269  
270 16 ▶ if(@{ $self->{fields} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{'fields'};}) { } 14 2 16
271 14 $self->{csv}->combine(@{ $self->{fields} })
272 14 ▶ or croak "CSV combine error: " . $self->{csv}->error_diag();
Branch: 1 branch
BranchTrueFalseTotal
$self->{"csv"}->combine(@{$$self{"fields"};}) or croak('CSV combine error: ' . $self->{"csv"}->error_diag) 14 0 14
273 14 $self->{file}[$self->{line_num}] = $self->{csv}->string;
274 } else {
275 2 $self->{file}[$self->{line_num}] = '';
276 }
277 }
278  
279 __END__
↑ Summary CSV/HoldRow.pm »