blib/lib/Text/CSV/Separator.pm

filter: Statementstmt 84.2% 128 / 152 Branchbran 74.4% 64 / 86 Conditioncond 70.8% 17 / 24 MC/DCmcdc 56.2% 9 / 16 Subroutinesub 90.9% 10 / 11 Podpod 100% 1 / 1 totaltotal 78.9% 229 / 290 CC 60 SCAR 40.4
CC
60
Cov
80%
get_separator
76.8
_regularity
7.0
_message
6.0
CRAP 57.1
↑ Summary
1 package Text::CSV::Separator;
2
3 4 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7
8 4 our $VERSION = '0.20';
9
10 4 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 our @EXPORT_OK = qw(get_separator);
13
14
15 18 ▶sub get_separator {
Subroutine get_separator: called CC=51 SCAR=43.4
Pod documented
16
17 18 my %options = @_;
18
19 18 my $file_path = $options{path};
20
21 # check options
22 18 my $echo;
23 18 ▶ if ($options{echo}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"echo"}) 0 18 18
24 0 $echo = 1;
25 0 print "\nDetecting field separator of $file_path\n";
26 }
27
28 18 my (@excluded, @included);
29 18 ▶ if (exists $options{exclude}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options{"exclude"}) 6 12 18
30 6 @excluded = @{$options{exclude}};
31 }
32
33 18 ▶ if (exists $options{include}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options{"include"}) 2 16 18
34 2 @included = @{$options{include}};
35 }
36
37 18 my ($lucky, $colon_timecol, $comma_decsep, $comma_groupsep);
38 18 ▶ if (exists $options{lucky} && $options{lucky} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options{'lucky'} and $options{'lucky'} == 1) { } 9 9 18
Condition66%2/3
exists $options{"lucky"} and $options{"lucky"} == 1
!ll&&!rl&&r
909
MC/DC50%1/2
exists $options{"lucky"} and $options{"lucky"} == 1
exists $options{"lucky"} $options{"lucky"} == 1
Truth table66%2/3
A and B
A= exists $options{"lucky"}B= $options{"lucky"} == 1
ABresult
0X0
100
111
39 9 $lucky = 1;
40 9 ▶ print "Scalar context...\n\n" if $echo;
Branch: 1 branch
BranchTrueFalseTotal
if $echo 0 9 9
41 } else {
42 9 $colon_timecol = $comma_decsep = $comma_groupsep = 1;
43 9 ▶ print "List context...\n\n" if $echo;
Branch: 1 branch
BranchTrueFalseTotal
if $echo 0 9 9
44 }
45
46 # options checked
47
48 # Default set of candidates
49 18 my @candidates = (',', ';', ':', '|', "\t");
50
51 18 my %survivors;
52 18 $survivors{$_} = [] foreach (@candidates);
53
54 18 ▶ if (@excluded > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@excluded > 0) 6 12 18
55 6 foreach (@excluded) {
56 12 delete $survivors{$_};
57 12 ▶ _message('deleted', $_) if $echo;
Branch: 1 branch
BranchTrueFalseTotal
if $echo 0 12 12
58 }
59 }
60
61 18 ▶ if (@included > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@included > 0) 2 16 18
62 2 foreach (@included) {
63 2 ▶ if (length($_) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ == 1) 2 0 2
64 2 $survivors{$_} = [];
65 }
66 2 ▶ _message('added', $_) if $echo;
Branch: 1 branch
BranchTrueFalseTotal
if $echo 0 2 2
67 }
68 }
69
70 18 ▶ if (keys %survivors == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %survivors == 0) 0 18 18
71 0 carp "No candidates left!";
72 0 return;
73 }
74
75 18 my $csv;
76 18 ▶ open ($csv, "<:crlf", $file_path) || croak "Couldn't open $file_path: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $csv, "<:crlf", $file_path or croak("Couldn't open ${file_path}: $!") 18 0 18
77
78 18 my $record_count = 0; # if $echo
79 18 ▶ while (<$csv>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $csv)) 5134 8 5142
80 5134 my $record = $_;
81 5134 chomp $record;
82
83 5134 ▶ if ($echo) {
Branch: 1 branch
BranchTrueFalseTotal
if ($echo) 0 5134 5134
84 0 $record_count++;
85 0 print "\nRecord #$record_count\n";
86 }
87
88 5134 foreach my $candidate (keys %survivors) {
89 10314 ▶ _message('candidate', $candidate) if $echo;
Branch: 1 branch
BranchTrueFalseTotal
if $echo 0 10314 10314
90
91 10314 my $rex = qr/\Q$candidate\E/;
92
93 10314 my $count = 0;
94 10314 ▶ $count++ while ($record =~ /$rex/g);
Branch: 1 branch
BranchTrueFalseTotal
$record =~ /$rex/g and do { ++$count } 44974 10314 55288
95
96 10314 ▶ print "Count: $count\n" if $echo;
Branch: 1 branch
BranchTrueFalseTotal
if $echo 0 10314 10314
97
98 10314 ▶ if ($count > 0 && !$lucky) {
Branch: 2 branches
BranchTrueFalseTotal
if ($count > 0 and not $lucky) { } 5130 5184 10314
elsif ($count == 0) { } 54 5130 5184
Condition100%3/3
$count > 0 and not $lucky
!ll&&!rl&&r
5451305130
MC/DC100%2/2
$count > 0 and not $lucky
$count > 0 not $lucky
Truth table100%3/3
A and not(B)
A= $count > 0B= not $lucky
ABresult
0X0
100
111
99 5130 push @{$survivors{$candidate}}, $count;
100 } elsif ($count == 0) {
101 54 delete $survivors{$candidate};
102 }
103
104 }
105
106 5134 ▶ if (!$lucky) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($lucky) 2567 2567 5134
107 2567 ▶ $colon_timecol = _regularity($record, 'timecol') if $colon_timecol;
Branch: 1 branch
BranchTrueFalseTotal
if $colon_timecol 1524 1043 2567
108 2567 ▶ $comma_decsep = _regularity($record, 'decsep') if $comma_decsep;
Branch: 1 branch
BranchTrueFalseTotal
if $comma_decsep 1046 1521 2567
109 2567 ▶ $comma_groupsep = _regularity($record, 'groupsep') if $comma_groupsep;
Branch: 1 branch
BranchTrueFalseTotal
if $comma_groupsep 9 2558 2567
110 }
111
112
113 5134 my @alive = keys %survivors;
114 5134 my $survivors_count = @alive;
115 5134 ▶ if ($survivors_count == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($survivors_count == 1) { } 8 5126 5134
elsif ($survivors_count == 0) { } 2 5124 5126
116 8 ▶ if ($echo) {
Branch: 1 branch
BranchTrueFalseTotal
if ($echo) 0 8 8
117 0 _message('detected', $alive[0]);
118 0 print "Returning control to caller...\n\n";
119 }
120 8 close $csv;
121 8 ▶ if (!$lucky) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $lucky) { } 4 4 8
122 4 return @alive;
123 } else {
124 4 return $alive[0];
125 }
126 } elsif ($survivors_count == 0) {
127 2 carp "\nNo candidates left!\n";
128 2 return;
129 }
130 }
131
132 # More than 1 survivor. 2nd pass to determine count variability
133 8 ▶ if ($lucky) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lucky) { } 4 4 8
134 4 ▶ print "\nSeveral candidates left\n" if $echo;
Branch: 1 branch
BranchTrueFalseTotal
if $echo 0 4 4
135 4 carp "\nBad luck. Couldn't determine the separator of $file_path.\n";
136 4 return;
137 } else {
138 4 ▶ print "\nVariability:\n\n" if $echo;
Branch: 1 branch
BranchTrueFalseTotal
if $echo 0 4 4
139 4 my %std_dev;
140 4 foreach my $candidate (keys %survivors) {
141 9 my $mean = _mean(@{$survivors{$candidate}});
142 9 $std_dev{$candidate} = _std_dev($mean, @{$survivors{$candidate}});
143 9 ▶ if ($echo) {
Branch: 1 branch
BranchTrueFalseTotal
if ($echo) 0 9 9
144 0 _message('candidate', $candidate);
145 0 print "Mean: $mean\tStd Dev: $std_dev{$candidate}\n\n";
146 }
147 }
148
149 4 ▶ print "Couldn't determine the separator\n" if $echo;
Branch: 1 branch
BranchTrueFalseTotal
if $echo 0 4 4
150
151 4 close $csv;
152
153 4 my @penalized;
154 4 ▶ if ($colon_timecol) {
Branch: 1 branch
BranchTrueFalseTotal
if ($colon_timecol) 1 3 4
155 1 ▶ print "Detected time column\n" if $echo;
Branch: 1 branch
BranchTrueFalseTotal
if $echo 0 1 1
156 1 delete $survivors{':'};
157 1 push @penalized, ':';
158 }
159
160 4 ▶ if ($comma_decsep || $comma_groupsep) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comma_decsep or $comma_groupsep) 1 3 4
Condition66%2/3
$comma_decsep or $comma_groupsep
l!l&&r!l&&!r
103
MC/DC50%1/2
$comma_decsep or $comma_groupsep
$comma_decsep $comma_groupsep
Truth table66%2/3
A or B
A= $comma_decsepB= $comma_groupsep
ABresult
1X1
011
000
161 1 delete $survivors{','};
162 1 push @penalized, ',';
163 1 ▶ if ($echo && $comma_decsep) {
Branch: 1 branch
BranchTrueFalseTotal
if ($echo and $comma_decsep) 0 1 1
Condition33%1/3
$echo and $comma_decsep
!ll&&!rl&&r
100
MC/DC0%0/2
$echo and $comma_decsep
$echo $comma_decsep
Truth table33%1/3
A and B
A= $echoB= $comma_decsep
ABresult
0X0
100
111
164 0 print "\nDetected comma-separated decimal numbers column\n";
165 }
166 1 ▶ if ($echo && $comma_groupsep) {
Branch: 1 branch
BranchTrueFalseTotal
if ($echo and $comma_groupsep) 0 1 1
Condition33%1/3
$echo and $comma_groupsep
!ll&&!rl&&r
100
MC/DC0%0/2
$echo and $comma_groupsep
$echo $comma_groupsep
Truth table33%1/3
A and B
A= $echoB= $comma_groupsep
ABresult
0X0
100
111
167 0 print "\nDetected comma-grouped numbers column\n";
168 }
169 }
170
171 4 my @alive = sort {$std_dev{$a} <=> $std_dev{$b}} keys %survivors;
172 0 push @alive, sort {$std_dev{$a} <=> $std_dev{$b}} @penalized;
173 4 ▶ if ($echo) {
Branch: 1 branch
BranchTrueFalseTotal
if ($echo) 0 4 4
174 0 print "Remaining candidates: ";
175 0 foreach my $left (@alive) {
176 0 _message('left', $left);
177 }
178 0 print "\n\nReturning control to caller...\n\n";
179 }
180 4 return @alive;
181 }
182 }
183
184 9 ▶sub _mean {
Subroutine _mean: called CC=2 SCAR=6.9
185 9 my @array = @_;
186
187 9 my $sum = 0;
188 9 $sum += $_ foreach (@array);
189
190 9 my $mean = $sum / scalar(@array);
191
192 9 return $mean;
193 }
194
195 9 ▶sub _std_dev {
Subroutine _std_dev: called CC=2 SCAR=6.9
196 9 my ($mean, @array) = @_;
197
198 9 my $sum = 0;
199 9 $sum += ($_ - $mean)**2 foreach (@array);
200
201 9 my $std_dev = sqrt( $sum / scalar(@array) );
202
203 9 return $std_dev;
204 }
205
206 2579 ▶sub _regularity {
Subroutine _regularity: called CC=7 SCAR=19.5
207 2579 my ($string, $kind) = @_;
208
209 2579 my $time_rx = qr/
210 (?:^|(?<=\s|[T,;|\t]))
211 (?:[01]?[0-9]|2[0-3]) # hours
212 :
213 (?:[0-5][0-9]) # minutes
214 (?::[0-5][0-9])? # seconds
215 (?:
216 Z
217 |
218 \.\d+
219 |
220 (?:\+|-)
221 (?:[01]?[0-9]|2[0-3])
222 :
223 (?:[0-5][0-9])
224 )?
225 (?=$|\s|[,;|\t])
226 /x;
227
228 2579 my $commadecsep_rx = qr/
229 (?:^|(?<=[^\d,.]))
230 (?:
231 [-+]?
232 (?:
233 \d{0,3}?(?:\.\d{3})*
234 |
235 \d+
236 )
237 ,\d+
238 )
239 (?=$|[^\d,.])
240 /x;
241
242 2579 my $commagroupsep_rx = qr/
243 (?:^|(?<=[^\d,.]))
244 (?:
245 [-+]?\d{0,3}?
246 (?:,\d{3})+
247 (?:\.\d+)?
248 )
249 (?=$|[^\d,.])
250 /x;
251
252
253 2579 ▶ return 0 if ($kind eq 'timecol' && $string !~ /$time_rx/);
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "timecol" and not $string =~ /$time_rx/ 8 2571 2579
Condition100%3/3
$kind eq "timecol" and not $string =~ /$time_rx/
!ll&&!rl&&r
105515168
MC/DC100%2/2
$kind eq "timecol" and not $string =~ /$time_rx/
$kind eq "timecol" not $string =~ /$time_rx/
Truth table100%3/3
A and not(B)
A= $kind eq "timecol"B= not $string =~ /$time_rx/
ABresult
0X0
100
111
254 2571 ▶ return 0 if ($kind eq 'decsep' && $string !~ /$commadecsep_rx/);
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "decsep" and not $string =~ /$commadecsep_rx/ 8 2563 2571
Condition100%3/3
$kind eq "decsep" and not $string =~ /$commadecsep_rx/
!ll&&!rl&&r
152510388
MC/DC100%2/2
$kind eq "decsep" and not $string =~ /$commadecsep_rx/
$kind eq "decsep" not $string =~ /$commadecsep_rx/
Truth table100%3/3
A and not(B)
A= $kind eq "decsep"B= not $string =~ /$commadecsep_rx/
ABresult
0X0
100
111
255 2563 ▶ return 0 if ($kind eq 'groupsep' && $string !~ /$commagroupsep_rx/);
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "groupsep" and not $string =~ /$commagroupsep_rx/ 9 2554 2563
Condition66%2/3
$kind eq "groupsep" and not $string =~ /$commagroupsep_rx/
!ll&&!rl&&r
255409
MC/DC50%1/2
$kind eq "groupsep" and not $string =~ /$commagroupsep_rx/
$kind eq "groupsep" not $string =~ /$commagroupsep_rx/
Truth table66%2/3
A and not(B)
A= $kind eq "groupsep"B= not $string =~ /$commagroupsep_rx/
ABresult
0X0
100
111
256
257 2554 return 1;
258 }
259
260 0 ▶sub _message {
Subroutine _message: not called CC=2 SCAR=17.9
261 0 my ($type, $candidate) = @_;
262
263 0 my $char;
264 0 ▶ if (ord $candidate == 9) { # tab character
Branch: 1 branch
BranchTrueFalseTotal
if (ord $candidate == 9) { } 0 0 0
265 0 $char = "\\t";
266 } else {
267 0 $char = $candidate;
268 }
269
270 0 my %message = (
271 deleted => "Deleted $char from candidates list\n",
272 added => "Added $char to candidates list\n",
273 candidate => "Candidate: $char\t",
274 detected => "\nSeparator detected: $char\n",
275 left => " $char ",
276 );
277
278 0 print $message{$type};
279 }
280
281 4 1;
282
283 __END__
↑ Summary