MechanicalTurk/DelimitedWriter.pm

filter: Statementstmt 93.1% 68 / 73 Branchbran 63.1% 24 / 38 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 5 totaltotal 82.4% 113 / 137 CC 22 SCAR 16.0
CC
22
Cov
84%
open
7.5
init
6.5
write
6.0
CRAP 5.0
« MechanicalTurk/QAPValidator/QAPValidatorMSXML.pm ↑ Summary MechanicalTurk/Template/PerlTemplate.pm »
1 package Net::Amazon::MechanicalTurk::DelimitedWriter;
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::DelimitedWriter->attributes(qw{
13 fieldSeparator
14 output
15 file
16 append
17 lazy
18 utf8
19 rowsWritten
20 autoflush
21 autoclose
22 });
23  
24 2 ▶sub init {
Subroutine init: called CC=5 SCAR=18.7
Pod undocumented
25 2 my $self = shift;
26 2 $self->setAttributes(@_);
27 2 $self->setAttributesIfNotDefined(
28 fieldSeparator => ',',
29 utf8 => 1,
30 lazy => 0,
31 append => 0,
32 autoflush => 0,
33 autoclose => 0
34 );
35 2 ▶ if (!defined $self->output) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->output) { } 2 0 2
36 2 ▶ if (!defined $self->file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->file) 0 2 2
37 0 Carp::croak("Either output or file must be specified.");
38 }
39 2 ▶ if (!$self->lazy) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->lazy) 2 0 2
40 2 $self->open;
41 }
42 }
43 else {
44 0 ▶ $self->output->autoflush(1) if $self->autoflush;
Branch: 1 branch
BranchTrueFalseTotal
if $self->autoflush 0 0 0
45 }
46 2 $self->rowsWritten(0);
47 }
48  
49 2 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=14.2
50 2 my $self = shift;
51 2 ▶ if ($self->autoclose) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->autoclose) { } 2 0 2
elsif ($self->output) { } 0 0 0
52 2 $self->close;
53 }
54 elsif ($self->output) {
55 0 $self->output->flush;
56 }
57 }
58  
59 4 ▶sub close {
Subroutine close: called CC=2 SCAR=6.9
Pod undocumented
60 4 my $self = shift;
61 4 ▶ if ($self->output) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->output) 2 2 4
62 2 $self->output->close;
63 2 $self->output(undef);
64 }
65 }
66  
67 16 ▶sub open {
Subroutine open: called CC=7 SCAR=20.2
Pod undocumented
68 16 my $self = shift;
69 16 ▶ if (defined $self->output) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->output) { } 14 2 16
70 14 return $self->output;
71 }
72 else {
73 2 ▶ my $mode = ($self->append) ? "a" : "w";
Branch: 1 branch
BranchTrueFalseTotal
$self->append ? : 0 2 2
74 2 my $out = IO::File->new($self->file, $mode);
75 2 ▶ if (!$out) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($out) 0 2 2
76 0 Carp::croak("Couldn't open " . $self->file . " - $!.");
77 }
78 2 ▶ if ($self->utf8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->utf8) 2 0 2
79 # By using utf8 these modules should be able to handle
80 # non-english answers with recent versions of perl.
81 2 eval { binmode($out, ":utf8") };
82 2 ▶ warn "Couldn't set filehandle to utf8." if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
83 }
84 2 ▶ $out->autoflush(1) if $self->autoflush;
Branch: 1 branch
BranchTrueFalseTotal
if $self->autoflush 0 2 2
85 2 $self->output($out);
86 2 $self->autoclose(1);
87 2 return $out;
88 }
89 }
90  
91 14 ▶sub write {
Subroutine write: called CC=6 SCAR=17.9
Pod undocumented
92 14 my $self = shift;
93 14 ▶ my $row = ($#_ == 0 and UNIVERSAL::isa($_[0], "ARRAY")) ? $_[0] : [@_];
Branch: 1 branch
BranchTrueFalseTotal
$#_ == 0 && UNIVERSAL::isa($_[0], 'ARRAY') ? : 4 10 14
Condition100%3/3
$#_ == 0 and UNIVERSAL::isa($_[0], "ARRAY")
!ll&&!rl&&r
824
MC/DC100%2/2
$#_ == 0 and UNIVERSAL::isa($_[0], "ARRAY")
$#_ == 0 UNIVERSAL::isa($_[0], "ARRAY")
Truth table100%3/3
A and B
A= $#_ == 0B= UNIVERSAL::isa($_[0], "ARRAY")
ABresult
0X0
100
111
94 14 my $out = $self->open;
95 14 my $rowsWritten = $self->rowsWritten;
96 14 ▶ if ($rowsWritten > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rowsWritten > 0) 12 2 14
97 12 print $out "\n";
98 }
99 14 my $fs = $self->fieldSeparator;
100 14 ▶ for (my $i=0; $i<=$#{$row}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$row;}) 32 14 46
101 32 ▶ if ($i > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 20 12 32
102 20 print $out $fs;
103 }
104 32 print $out $self->formatCell($row->[$i]);
105 }
106 14 $self->rowsWritten($rowsWritten+1);
107 }
108  
109 32 ▶sub formatCell {
Subroutine formatCell: called CC=4 SCAR=14.0
Pod undocumented
110 32 my ($self, $cell) = @_;
111 32 my $fs = $self->fieldSeparator;
112 32 ▶ if (!defined $cell) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cell) 0 32 32
113 0 return '';
114 }
115 32 ▶ if (index($cell, $fs) >= 0 or $cell =~ /[\n"]/s) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($cell, $fs) != -1 or $cell =~ /[\n"]/s) { } 7 25 32
Condition100%3/3
index($cell, $fs) != -1 or $cell =~ /[\n"]/s
l!l&&r!l&&!r
3425
MC/DC100%2/2
index($cell, $fs) != -1 or $cell =~ /[\n"]/s
index($cell, $fs) != -1 $cell =~ /[\n"]/s
Truth table100%3/3
A or B
A= index($cell, $fs) != -1B= $cell =~ /[\n"]/s
ABresult
1X1
011
000
116 7 $cell =~ s/"/""/gs;
117 7 return '"' . $cell . '"';
118 }
119 else {
120 25 return $cell;
121 }
122 }
123  
124 1 return 1;
« MechanicalTurk/QAPValidator/QAPValidatorMSXML.pm ↑ Summary MechanicalTurk/Template/PerlTemplate.pm »