blib/lib/Text/CSV_Multiline.pm

filter: Statementstmt 76.9% 40 / 52 Branchbran 62.5% 10 / 16 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 75.0% 6 / 8 Podpod 0.0% 0 / 4 totaltotal 63.3% 57 / 90 CC 11 SCAR 20.6
CC
11
Cov
69%
csv_quote
20.0
csv_read_record
7.3
csv_unquote
2.0
CRAP 7.8
↑ Summary
1 package Text::CSV_Multiline;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use base "Exporter";
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Fcntl ":seek";
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 our @EXPORT = qw(
10 csv_quote
11 csv_unquote
12 csv_read_record
13 csv_write_record
14 );
15  
16 2 our $VERSION = 0.01;
17 2 our $ALWAYS_USE_QUOTES = 0;
18  
19 sub csv_quote
20 0 ▶{
Subroutine csv_quote: not called CC=4 SCAR=30.0
Pod undocumented
21 0 my $value = shift;
22 0 ▶ $value = "" if not defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
23 0 ▶ if ($value =~ /^[\w\d.]*$/s && !$ALWAYS_USE_QUOTES)
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^[\w\d.]*$/s and not $Text::CSV_Multiline::ALWAYS_USE_QUOTES) 0 0 0
Condition0%0/3
$value =~ /^[\w\d.]*$/s and not $Text::CSV_Multiline::ALWAYS_USE_QUOTES
!ll&&!rl&&r
000
MC/DC0%0/2
$value =~ /^[\w\d.]*$/s and not $Text::CSV_Multiline::ALWAYS_USE_QUOTES
$value =~ /^[\w\d.]*$/s not $Text::CSV_Multiline::ALWAYS_USE_QUOTES
Truth table0%0/3
A and not(B)
A= $value =~ /^[\w\d.]*$/sB= not $Text::CSV_Multiline::ALWAYS_USE_QUOTES
ABresult
0X0
100
111
24 {
25 0 return $value;
26 }
27 0 $value =~ s/"/""/gs;
28 0 return "\"$value\"";
29 }
30  
31 sub csv_unquote
32 16 ▶{
Subroutine csv_unquote: called CC=2 SCAR=6.9
Pod undocumented
33 16 my $quoted = shift;
34 16 $quoted =~ s/^\s+//s;
35 16 $quoted =~ s/\s+$//s;
36 16 ▶ if ($quoted =~ /^"(.*)"$/s)
Branch: 1 branch
BranchTrueFalseTotal
if ($quoted =~ /^"(.*)"$/s) 3 13 16
37 {
38 3 $quoted = $1;
39 3 $quoted =~ s/""/"/gs;
40 }
41 #print STDERR "found field: >$quoted<\n";
42 16 return $quoted;
43 }
44  
45 sub csv_read_record
46 5 ▶{
Subroutine csv_read_record: called CC=7 SCAR=19.9
Pod undocumented
47 5 my $fh = shift;
48 5 my @parts = ();
49 5 my $line = "";
50 5 my $num_lines = 0;
51 5 local $_;
52 5 ▶ while (defined($_ = (ref($fh) && $fh->can("readline"))? $fh->readline : <$fh>))
Branch: 2 branches
BranchTrueFalseTotal
if (defined($_ = ref $fh && $fh->can("readline") ? $fh->readline : readline $fh)) 18 1 19
ref $fh && $fh->can('readline') ? : 0 19 19
Condition33%1/3
ref $fh and $fh->can("readline")
!ll&&!rl&&r
0190
MC/DC0%0/2
ref $fh and $fh->can("readline")
ref $fh $fh->can("readline")
Truth table33%1/3
A and B
A= ref $fhB= $fh->can("readline")
ABresult
0X0
100
111
53 {
54 18 $line .= $_;
55 18 my $quoted_field = qr/"(?:[^"]|"")*"/;
56 18 my $unquoted_field = qr/[^,"]*/;
57 18 my $field = qr/\s*(?:$quoted_field|$unquoted_field)\s*/;
58  
59 18 ▶ while ($line =~ s/^($field),//s)
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ s/^($field),//s) 12 18 30
60 {
61 12 push @parts, csv_unquote($1);
62 }
63  
64 18 ▶ if ($line =~ /^($field)$/s)
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^($field)$/s) 4 14 18
65 {
66 # last field
67 4 push @parts, csv_unquote($1);
68 4 return @parts;
69 }
70 }
71 1 ▶ if (length($line))
Branch: 1 branch
BranchTrueFalseTotal
if (length $line) 0 1 1
72 {
73 0 warn "detected eof before last field was finished\n";
74 0 warn "-->$line<--\n";
75 }
76 1 return @parts;
77 }
78  
79 sub csv_write_record
80 0 ▶{
Subroutine csv_write_record: not called CC=1 SCAR=6.9
Pod undocumented
81 0 my $fh = shift;
82 0 my @values = @_;
83 0 print $fh join(",", map { csv_quote($_) } @values) . "\n";
84 }
85  
86 2 1;
87 __END__
↑ Summary