MechanicalTurk/DelimitedReader.pm

filter: Statementstmt 95.4% 84 / 88 Branchbran 80.9% 34 / 42 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 3 totaltotal 89.8% 132 / 147 CC 23 SCAR 23.1
CC
23
Cov
91%
next
16.0
init
6.9
DESTROY
2.0
CRAP 10.0
« MechanicalTurk/Command/ParseAssignmentAnswer.pm ↑ Summary MechanicalTurk/IOUtil.pm »
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
BranchTrueFalseTotal
if (not defined $self->input) { } 2 0 2
38 2 ▶ if (!defined $self->file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->file) 0 2 2
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
BranchTrueFalseTotal
unless ($in) 0 2 2
43 0 Carp::croak("Couldn't open " . $self->file . " - $!.");
44 }
45 2 ▶ if ($self->utf8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->utf8) 2 0 2
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
BranchTrueFalseTotal
if $@ 0 2 2
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
BranchTrueFalseTotal
if ($self->autoclose) 2 0 2
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
BranchTrueFalseTotal
if ($self->input) 2 2 4
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
BranchTrueFalseTotal
unless $self->input 2 22 24
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
BranchTrueFalseTotal
unless (defined $c) 2 200 202
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
BranchTrueFalseTotal
if $c eq "\r" 0 200 200
97
98 200 ▶ if ($quotedCell) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quotedCell) { } 100 100 200
99 100 ▶ if ($c eq "\n") {
Branch: 3 branches
BranchTrueFalseTotal
if ($c eq "\n") { } 8 92 100
elsif ($c eq $fs) { } 6 86 92
elsif ($c eq '"') { } 14 72 86
100 8 ▶ if ($lastWasQuote) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lastWasQuote) 2 6 8
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
BranchTrueFalseTotal
if ($lastWasQuote) { } 4 2 6
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
BranchTrueFalseTotal
if ($lastWasQuote) { } 4 10 14
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
BranchTrueFalseTotal
if ($lastWasQuote) 0 72 72
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
BranchTrueFalseTotal
if ($cell eq '' and $c eq '"') { } 6 94 100
elsif ($c eq "\n") { } 18 76 94
elsif ($c eq $fs) { } 10 66 76
Condition100%3/3
$cell eq "" and $c eq "\""
!ll&&!rl&&r
66286
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 "\""
ABresult
0X0
100
111
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;
« MechanicalTurk/Command/ParseAssignmentAnswer.pm ↑ Summary MechanicalTurk/IOUtil.pm »