| 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
| |||||||||||||||||||||||||||||||||
| 23 | 0 | ▶ | if ($value =~ /^[\w\d.]*$/s && !$ALWAYS_USE_QUOTES) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$value =~ /^[\w\d.]*$/s and not $Text::CSV_Multiline::ALWAYS_USE_QUOTES
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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
ref $fh and $fh->can("readline")
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")
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 60 | { | ||||||||||||||||||||||||||||||||
| 61 | 12 | push @parts, csv_unquote($1); | |||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | 18 | ▶ | if ($line =~ /^($field)$/s) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||