« 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
| |||||||||||||||||||||||||||||
| 36 | 2 | ▶ | if (!defined $self->file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 0 | Carp::croak("Either output or file must be specified."); | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | 2 | ▶ | if (!$self->lazy) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 2 | $self->open; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | else { | ||||||||||||||||||||||||||||
| 44 | 0 | ▶ | $self->output->autoflush(1) if $self->autoflush; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 70 | 14 | return $self->output; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | else { | ||||||||||||||||||||||||||||
| 73 | 2 | ▶ | my $mode = ($self->append) ? "a" : "w"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 2 | my $out = IO::File->new($self->file, $mode); | |||||||||||||||||||||||||||
| 75 | 2 | ▶ | if (!$out) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 0 | Carp::croak("Couldn't open " . $self->file . " - $!."); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | 2 | ▶ | if ($self->utf8) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 2 | ▶ | $out->autoflush(1) if $self->autoflush; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$#_ == 0 and UNIVERSAL::isa($_[0], "ARRAY")
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")
| |||||||||||||||||||||||||||||
| 94 | 14 | my $out = $self->open; | |||||||||||||||||||||||||||
| 95 | 14 | my $rowsWritten = $self->rowsWritten; | |||||||||||||||||||||||||||
| 96 | 14 | ▶ | if ($rowsWritten > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 12 | print $out "\n"; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 14 | my $fs = $self->fieldSeparator; | |||||||||||||||||||||||||||
| 100 | 14 | ▶ | for (my $i=0; $i<=$#{$row}; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 32 | ▶ | if ($i > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 113 | 0 | return ''; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | 32 | ▶ | if (index($cell, $fs) >= 0 or $cell =~ /[\n"]/s) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
index($cell, $fs) != -1 or $cell =~ /[\n"]/s
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
| |||||||||||||||||||||||||||||
| 116 | 7 | $cell =~ s/"/""/gs; | |||||||||||||||||||||||||||
| 117 | 7 | return '"' . $cell . '"'; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | else { | ||||||||||||||||||||||||||||
| 120 | 25 | return $cell; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 1 | return 1; | |||||||||||||||||||||||||||