CSV.pm

filter: Statementstmt 100% 126 / 126 Branchbran 98.0% 51 / 52 Conditioncond 92.8% 13 / 14 MC/DCmcdc 88.8% 8 / 9 Subroutinesub 100% 18 / 18 Podpod 100% 2 / 2 totaltotal 98.6% 218 / 221 CC 34 SCAR 26.2
CC
34
Cov
99%
_open
25.0
READLINE
7.0
header
3.0
CRAP 13.7
↑ Summary CSV/Hash.pm »
1 package Tie::Handle::CSV;
2  
3 11 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use Text::CSV_XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 11 ▶use Tie::Handle::CSV::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use Tie::Handle::CSV::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 11 our $VERSION = '0.15';
16  
17 sub new
18 28 ▶ {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
19 28 my $class = shift;
20 28 my $self = bless gensym(), $class;
21 28 tie *$self, $self;
22 28 $self->_open(@_);
23 19 return $self;
24 }
25  
26 sub TIEHANDLE
27 37 ▶ {
Subroutine TIEHANDLE: called CC=2 SCAR=6.9
28 37 ▶ return $_[0] if ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 28 9 37
29 9 my $class = shift;
30 9 return $class->new(@_);
31 }
32  
33 sub _open
34 28 ▶ {
Subroutine _open: called CC=25 SCAR=32.2
35 28 my ($self, @opts) = @_;
36  
37 28 my ($file, %opts, $csv_fh);
38  
39 ## if an odd number of options are given,
40 ## assume the first arg is the file name
41 28 ▶ if (@opts % 2)
Branch: 1 branch
BranchTrueFalseTotal
if (@opts % 2) { } 23 5 28
42 {
43 23 $file = shift @opts;
44 23 %opts = @opts;
45 23 $opts{'file'} = $file;
46 }
47 else
48 {
49 5 %opts = @opts;
50 }
51  
52 ## support old 'openmode' option key
53 28 ▶ if ( exists $opts{'openmode'} && ! exists $opts{'open_mode'} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"openmode"} and not exists $opts{"open_mode"}) 1 27 28
Condition100%3/3
exists $opts{"openmode"} and not exists $opts{"open_mode"}
!ll&&!rl&&r
2611
MC/DC100%2/2
exists $opts{"openmode"} and not exists $opts{"open_mode"}
exists $opts{"openmode"} not exists $opts{"open_mode"}
Truth table100%3/3
A and not(B)
A= exists $opts{"openmode"}B= not exists $opts{"open_mode"}
ABresult
0X0
100
111
54 {
55 1 $opts{'open_mode'} = $opts{'openmode'};
56 }
57  
58 ## support old 'stringify' option key
59 28 ▶ if ( exists $opts{'stringify'} && ! exists $opts{'simple_reads'} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"stringify"} and not exists $opts{"simple_reads"}) 1 27 28
Condition100%3/3
exists $opts{"stringify"} and not exists $opts{"simple_reads"}
!ll&&!rl&&r
2611
MC/DC100%2/2
exists $opts{"stringify"} and not exists $opts{"simple_reads"}
exists $opts{"stringify"} not exists $opts{"simple_reads"}
Truth table100%3/3
A and not(B)
A= exists $opts{"stringify"}B= not exists $opts{"simple_reads"}
ABresult
0X0
100
111
60 {
61 1 $opts{'simple_reads'} = ! $opts{'stringify'};
62 }
63  
64 28 ▶ my $file_ref_type = Scalar::Util::reftype( $opts{'file'} ) || '';
Condition100%2/2
builtin::reftype($opts{'file'}) || ''
l!l
127
MC/DC100%1/1
builtin::reftype($opts{'file'}) || ''
builtin::reftype($opts{'file'})
Truth table100%2/2
A
A= builtin::reftype($opts{'file'})
Aresult
11
00
65  
66 28 ▶ if ( $file_ref_type eq 'GLOB' )
Branch: 1 branch
BranchTrueFalseTotal
if ($file_ref_type eq 'GLOB') { } 1 27 28
67 {
68 1 $csv_fh = $opts{'file'};
69 }
70 else
71 {
72  
73 ## use 3-arg open if 'open_mode' is specified,
74 ## otherwise use 2-arg to work with STDIN via '-'
75 27 ▶ if ( defined $opts{'open_mode'} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{'open_mode'}) { } 5 22 27
76 {
77 5 ▶ open( $csv_fh, $opts{'open_mode'}, $opts{'file'} )
Branch: 1 branch
BranchTrueFalseTotal
open $csv_fh, $opts{"open_mode"}, $opts{"file"} or croak(qq[$!: $opts{"file"}]) 4 1 5
78 || croak "$!: $opts{'file'}";
79 }
80 else
81 {
82 22 ▶ open( $csv_fh, $opts{'file'} ) || croak "$!: $opts{'file'}";
Branch: 1 branch
BranchTrueFalseTotal
open $csv_fh, $opts{"file"} or croak(qq[$!: $opts{"file"}]) 15 7 22
83 }
84  
85 }
86  
87 ## establish the csv object
88 ## use given sep_char when possible
89 20 ▶ if ( $opts{'csv_parser'} )
Branch: 2 branches
BranchTrueFalseTotal
if ($opts{'csv_parser'}) { } 2 18 20
elsif (defined $opts{'sep_char'}) { } 2 16 18
90 {
91 2 ▶ if ( ref $opts{'csv_parser'} ne 'Text::CSV_XS' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"csv_parser"} ne "Text::CSV_XS") 1 1 2
92 {
93 1 confess "'csv_parser' is not an instance of 'Text::CSV_XS'";
94 }
95 }
96 elsif ( defined $opts{'sep_char'} )
97 {
98 $opts{'csv_parser'} =
99 2 Text::CSV_XS->new( { sep_char => $opts{'sep_char'}, binary => 1 } );
100 }
101 else
102 {
103 16 $opts{'csv_parser'} = Text::CSV_XS->new( { binary => 1 } );
104 }
105  
106 19 ▶ $opts{'header'} = 1 unless exists $opts{'header'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"header"} 5 14 19
107  
108 19 ▶ if ( $opts{'header'} )
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"header"}) 14 5 19
109 {
110  
111 14 ▶ if ( ref $opts{'header'} ne 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"header"} ne "ARRAY") 9 5 14
112 {
113 9 my $header_line = <$csv_fh>;
114 $opts{'csv_parser'}->parse($header_line)
115 9 ▶ || croak $opts{'csv_parser'}->error_input();
Branch: 1 branch
BranchTrueFalseTotal
$opts{"csv_parser"}->parse($header_line) or croak($opts{"csv_parser"}->error_input) 9 0 9
116 9 $opts{'header'} = [ $opts{'csv_parser'}->fields() ];
117 }
118  
119 14 $opts{'orig_header'} = [ @{ $opts{'header'} } ];
120  
121 ## support old 'force_lower' option key
122 14 ▶ if ( $opts{'force_lower'} && ! $opts{'key_case'} )
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"force_lower"} and not $opts{"key_case"}) 4 10 14
Condition100%3/3
$opts{"force_lower"} and not $opts{"key_case"}
!ll&&!rl&&r
914
MC/DC100%2/2
$opts{"force_lower"} and not $opts{"key_case"}
$opts{"force_lower"} not $opts{"key_case"}
Truth table100%3/3
A and not(B)
A= $opts{"force_lower"}B= not $opts{"key_case"}
ABresult
0X0
100
111
123 {
124 4 $opts{'key_case'} = 'lower';
125 }
126  
127 14 ▶ if ( $opts{'key_case'} )
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"key_case"}) 9 5 14
128 {
129  
130 9 ▶ if ( lc $opts{'key_case'} eq 'lower' )
Branch: 2 branches
BranchTrueFalseTotal
if (lc $opts{'key_case'} eq 'lower') { } 6 3 9
elsif (lc $opts{'key_case'} eq 'upper') { } 1 2 3
131 {
132 6 for my $header ( @{ $opts{'header'} } )
133 {
134 18 $header = lc $header;
135 }
136 }
137 elsif ( lc $opts{'key_case'} eq 'upper' )
138 {
139 1 for my $header ( @{ $opts{'header'} } )
140 {
141 3 $header = uc $header;
142 }
143 }
144  
145 }
146  
147 }
148  
149 19 *$self->{handle} = $csv_fh;
150 19 *$self->{opts} = \%opts;
151 }
152  
153 sub READLINE
154 64 ▶ {
Subroutine READLINE: called CC=7 SCAR=19.5
155 64 my ($self) = @_;
156  
157 64 my $opts = *$self->{'opts'};
158  
159 64 ▶ if (wantarray)
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 1 63 64
160 {
161  
162 1 my @parsed_lines;
163  
164 1 ▶ while (my $parsed_line = $self->READLINE)
Branch: 1 branch
BranchTrueFalseTotal
if (my $parsed_line = $self->READLINE) 3 1 4
165 {
166 3 push @parsed_lines, $parsed_line;
167 }
168  
169 1 return @parsed_lines;
170  
171 }
172 else
173 {
174 63 my $cols = $opts->{'csv_parser'}->getline(*$self->{'handle'});
175 63 ▶ if (defined $cols)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cols) 44 19 63
176 {
177 44 ▶ if ( $opts->{'header'} )
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'header'}) { } 33 11 44
178 {
179 33 my $parsed_line;
180  
181 33 ▶ if ( $opts->{'simple_reads'} )
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'simple_reads'}) { } 11 22 33
182 {
183 11 @{ $parsed_line }{ @{ $opts->{'header'} } } = @{ $cols };
184 }
185 else
186 {
187 22 $parsed_line = Tie::Handle::CSV::Hash->_new($self);
188 22 $parsed_line->_init_store( $cols );
189 }
190  
191 33 return $parsed_line;
192 }
193 else
194 {
195 11 my $parsed_line;
196  
197 11 ▶ if ( $opts->{'simple_reads'} )
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'simple_reads'}) { } 3 8 11
198 {
199 3 @{ $parsed_line } = @{ $cols };
200 }
201 else
202 {
203 8 $parsed_line = Tie::Handle::CSV::Array->_new($self);
204 8 $parsed_line->_init_store( $cols );
205 }
206  
207 11 return $parsed_line;
208 }
209 }
210  
211 }
212  
213 19 return;
214  
215 }
216  
217 sub CLOSE
218 19 ▶ {
Subroutine CLOSE: called CC=1 SCAR=0.0
219 19 my ($self) = @_;
220 19 return close *$self->{'handle'};
221 }
222  
223 sub PRINT
224 1 ▶ {
Subroutine PRINT: called CC=1 SCAR=0.0
225 1 my ($self, @list) = @_;
226 1 my $handle = *$self->{'handle'};
227 1 return print $handle @list;
228 }
229  
230 sub SEEK
231 3 ▶ {
Subroutine SEEK: called CC=1 SCAR=0.0
232 3 my ($self, $position, $whence) = @_;
233 3 return seek *$self->{'handle'}, $position, $whence;
234 }
235  
236 sub TELL
237 2 ▶ {
Subroutine TELL: called CC=1 SCAR=0.0
238 2 my ($self) = @_;
239 2 return tell *$self->{'handle'};
240 }
241  
242 sub header
243 5 ▶ {
Subroutine header: called CC=3 SCAR=11.0
Pod documented
244 5 my ($self) = @_;
245 5 my $opts = *$self->{opts};
246 5 my $header = $opts->{orig_header};
247 5 my $parser = $opts->{csv_parser};
248  
249 5 ▶ if ( ! $header || ref $header ne 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (not $header or ref $header ne "ARRAY") 1 4 5
Condition66%2/3
not $header or ref $header ne "ARRAY"
l!l&&r!l&&!r
104
MC/DC50%1/2
not $header or ref $header ne "ARRAY"
not $header ref $header ne "ARRAY"
Truth table66%2/3
not(A) or B
A= not $headerB= ref $header ne "ARRAY"
ABresult
1X1
011
000
250 {
251 1 croak "handle does not contain a header";
252 }
253  
254 4 my $header_array = Tie::Handle::CSV::Array->_new($self);
255 4 @{ $header_array } = @{$header};
256 4 return $header_array;
257 }
258  
259 11 1;
260  
261 __END__
↑ Summary CSV/Hash.pm »