| 1 | package Net::Amazon::MechanicalTurk::DelimitedReader; | ||||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use IO::File; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Net::Amazon::MechanicalTurk::BaseObject; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||
| 8 | 1 | our $VERSION = '1.00'; | |||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||
| 10 | 1 | our @ISA = qw{ Net::Amazon::MechanicalTurk::BaseObject }; | |||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||
| 12 | 1 | Net::Amazon::MechanicalTurk::DelimitedReader->attributes(qw{ | |||||||||||||||||||||||||||||||||||
| 13 | fieldSeparator | ||||||||||||||||||||||||||||||||||||
| 14 | input | ||||||||||||||||||||||||||||||||||||
| 15 | file | ||||||||||||||||||||||||||||||||||||
| 16 | utf8 | ||||||||||||||||||||||||||||||||||||
| 17 | autoclose | ||||||||||||||||||||||||||||||||||||
| 18 | }); | ||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||
| 20 | # The following CPAN modules do not support new lines in a column: | ||||||||||||||||||||||||||||||||||||
| 21 | # (So I wrote this class) | ||||||||||||||||||||||||||||||||||||
| 22 | # Text::CSV | ||||||||||||||||||||||||||||||||||||
| 23 | # Text::CSV_XS | ||||||||||||||||||||||||||||||||||||
| 24 | # Text::CSV_PP | ||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||
| 26 | 2 | ▶ | sub init { | ||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=6 SCAR=19.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 27 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 28 | 2 | $self->setAttributes(@_); | |||||||||||||||||||||||||||||||||||
| 29 | 2 | $self->setAttributesIfNotDefined( | |||||||||||||||||||||||||||||||||||
| 30 | fieldSeparator => ',', | ||||||||||||||||||||||||||||||||||||
| 31 | utf8 => 1, | ||||||||||||||||||||||||||||||||||||
| 32 | autoclose => 0 | ||||||||||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||||||||||
| 34 | 2 | $self->assertRequiredAttributes(qw{ | |||||||||||||||||||||||||||||||||||
| 35 | fieldSeparator | ||||||||||||||||||||||||||||||||||||
| 36 | }); | ||||||||||||||||||||||||||||||||||||
| 37 | 2 | ▶ | if (!defined $self->input) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 38 | 2 | ▶ | if (!defined $self->file) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 39 | 0 | Carp::croak("Either input or file must be specified."); | |||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||
| 41 | 2 | my $in = IO::File->new($self->file, "r"); | |||||||||||||||||||||||||||||||||||
| 42 | 2 | ▶ | if (!$in) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 43 | 0 | Carp::croak("Couldn't open " . $self->file . " - $!."); | |||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||
| 45 | 2 | ▶ | if ($self->utf8) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 46 | # By using utf8 these modules should be able to handle | ||||||||||||||||||||||||||||||||||||
| 47 | # non-english answers with recent versions of perl. | ||||||||||||||||||||||||||||||||||||
| 48 | 2 | eval { binmode($in, ":utf8") }; | |||||||||||||||||||||||||||||||||||
| 49 | 2 | ▶ | warn "Couldn't set filehandle to utf8." if $@; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||
| 51 | 2 | $self->autoclose(1); | |||||||||||||||||||||||||||||||||||
| 52 | 2 | $self->input($in); | |||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||
| 54 | else { | ||||||||||||||||||||||||||||||||||||
| 55 | 0 | $self->file(sprintf "%s", $self->input); | |||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||
| 59 | 2 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||
| 60 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 61 | 2 | ▶ | if ($self->autoclose) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 62 | 2 | $self->close; | |||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||
| 66 | 4 | ▶ | sub close { | ||||||||||||||||||||||||||||||||||
|
Subroutine
close: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 67 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 68 | 4 | ▶ | if ($self->input) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 69 | 2 | $self->input->close; | |||||||||||||||||||||||||||||||||||
| 70 | 2 | $self->input(undef); | |||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||
| 74 | 24 | ▶ | sub next { | ||||||||||||||||||||||||||||||||||
|
Subroutine
next: called CC=16 SCAR=27.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 75 | 24 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 76 | 24 | my $in = $self->input; | |||||||||||||||||||||||||||||||||||
| 77 | 24 | my $fs = $self->fieldSeparator; | |||||||||||||||||||||||||||||||||||
| 78 | 24 | my $row = []; | |||||||||||||||||||||||||||||||||||
| 79 | 24 | my $lastWasQuote = 0; | |||||||||||||||||||||||||||||||||||
| 80 | 24 | my $quotedCell = 0; | |||||||||||||||||||||||||||||||||||
| 81 | 24 | my $cell = ''; | |||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||
| 83 | 24 | ▶ | return undef unless $self->input; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||
| 85 | 22 | while (1) { | |||||||||||||||||||||||||||||||||||
| 86 | 202 | my $c = getc($in); | |||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||
| 88 | # Handle end of input | ||||||||||||||||||||||||||||||||||||
| 89 | 202 | ▶ | if (!defined($c)) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 90 | 2 | push(@$row, $cell); | |||||||||||||||||||||||||||||||||||
| 91 | 2 | $self->close; | |||||||||||||||||||||||||||||||||||
| 92 | 2 | $self->input(undef); | |||||||||||||||||||||||||||||||||||
| 93 | 2 | return $row; | |||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||
| 96 | 200 | ▶ | next if ($c eq "\r"); # just throw away \r | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||
| 98 | 200 | ▶ | if ($quotedCell) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 99 | 100 | ▶ | if ($c eq "\n") { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||
| 100 | 8 | ▶ | if ($lastWasQuote) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 101 | 2 | push(@$row, $cell); | |||||||||||||||||||||||||||||||||||
| 102 | 2 | return $row; | |||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||
| 104 | 6 | $cell .= "\n"; | |||||||||||||||||||||||||||||||||||
| 105 | 6 | $lastWasQuote = 0; | |||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||
| 107 | elsif ($c eq $fs) { | ||||||||||||||||||||||||||||||||||||
| 108 | 6 | ▶ | if ($lastWasQuote) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 109 | 4 | push(@$row, $cell); | |||||||||||||||||||||||||||||||||||
| 110 | 4 | $cell = ''; | |||||||||||||||||||||||||||||||||||
| 111 | 4 | $lastWasQuote = 0; | |||||||||||||||||||||||||||||||||||
| 112 | 4 | $quotedCell = 0; | |||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||
| 114 | else { | ||||||||||||||||||||||||||||||||||||
| 115 | 2 | $cell .= $c; | |||||||||||||||||||||||||||||||||||
| 116 | 2 | $lastWasQuote = 0; | |||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||
| 119 | elsif ($c eq '"') { | ||||||||||||||||||||||||||||||||||||
| 120 | 14 | ▶ | if ($lastWasQuote) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 121 | 4 | $cell .= $c; | |||||||||||||||||||||||||||||||||||
| 122 | 4 | $lastWasQuote = 0; | |||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||
| 124 | else { | ||||||||||||||||||||||||||||||||||||
| 125 | 10 | $lastWasQuote = 1; | |||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||
| 128 | else { | ||||||||||||||||||||||||||||||||||||
| 129 | 72 | ▶ | if ($lastWasQuote) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 130 | 0 | warn "Single quote found in cell which was not escaped.\n"; | |||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||
| 132 | 72 | $cell .= $c; | |||||||||||||||||||||||||||||||||||
| 133 | 72 | $lastWasQuote = 0; | |||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||
| 136 | else { | ||||||||||||||||||||||||||||||||||||
| 137 | 100 | ▶ | if ($cell eq '' and $c eq '"') { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition100%3/3
$cell eq "" and $c eq "\""
MC/DC100%2/2
$cell eq "" and $c eq "\""
$cell eq ""
$c eq "\""
Truth table100%3/3
A and B
A= $cell eq ""B= $c eq "\""
| |||||||||||||||||||||||||||||||||||||
| 138 | 6 | $quotedCell = 1; | |||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||
| 140 | elsif ($c eq "\n") { | ||||||||||||||||||||||||||||||||||||
| 141 | 18 | push(@$row, $cell); | |||||||||||||||||||||||||||||||||||
| 142 | 18 | return $row; | |||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||
| 144 | elsif ($c eq $fs) { | ||||||||||||||||||||||||||||||||||||
| 145 | 10 | push(@$row, $cell); | |||||||||||||||||||||||||||||||||||
| 146 | 10 | $cell = ''; | |||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||
| 148 | else { | ||||||||||||||||||||||||||||||||||||
| 149 | 66 | $cell .= $c; | |||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||
| 155 | 1 | return 1; | |||||||||||||||||||||||||||||||||||