MechanicalTurk/BaseObject.pm

filter: Statementstmt 52.8% 64 / 121 Branchbran 27.0% 13 / 48 Conditioncond 8.3% 2 / 24 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 72.7% 16 / 22 Podpod 0.0% 0 / 11 totaltotal 39.4% 95 / 241 CC 38 SCAR 37.6
CC
38
Cov
41%
debug
156.0
debugMessage
56.0
trySetAttributes
30.0
CRAP 43.0
« MechanicalTurk/Command/RetrieveResults.pm ↑ Summary MechanicalTurk/ModuleUtil.pm »
1 package Net::Amazon::MechanicalTurk::BaseObject;
2 31 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 31 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 31 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 31 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 31 our $VERSION = '1.00';
8  
9 31 ▶use constant USE_QUALIFIED_ATTRIBUTE_NAMES => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 31 our %CLASS_DEBUG;
12  
13 7 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
14 7 my $class = shift;
15 7 my $self = bless {}, $class;
16 7 $self->init(@_);
17 7 return $self;
18 }
19  
20 0 ▶sub init {}
Subroutine init: not called CC=1 SCAR=0.0
Pod undocumented
21  
22 0 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
23  
24 4 ▶sub assertRequiredAttributes {
Subroutine assertRequiredAttributes: called CC=3 SCAR=12.0
Pod undocumented
25 4 my $self = shift;
26 4 foreach my $attr (@_) {
27 4 ▶ if (!defined($self->$attr)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->$attr) 0 4 4
28 0 Carp::croak("Required attribute ${attr} was not set.");
29 }
30 }
31 }
32  
33 4 ▶sub setAttributesIfNotDefined {
Subroutine setAttributesIfNotDefined: called CC=5 SCAR=16.8
Pod undocumented
34 4 my $self = shift;
35 0 ▶ my %attrs = ($#_ == 0) ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
$#_ == 0 ? : 0 4 4
36 4 ▶ while (my ($attr,$value) = each %attrs) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %attrs) 18 4 22
37 18 ▶ eval { $self->$attr($value) unless defined($self->$attr); };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->$attr 15 3 18
38 0 ▶ if ($@) { Carp::croak("Can't set attribute $attr - $@"); }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 18 18
39 }
40 }
41  
42 7 ▶sub setAttributes {
Subroutine setAttributes: called CC=4 SCAR=14.8
Pod undocumented
43 7 my $self = shift;
44 0 ▶ my %attrs = ($#_ == 0) ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
$#_ == 0 ? : 0 7 7
45 7 ▶ while (my ($attr,$value) = each %attrs) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %attrs) 9 7 16
46 9 eval { $self->$attr($value); };
47 0 ▶ if ($@) { Carp::croak("Can't set attribute $attr - $@"); }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 9 9
48 }
49 }
50  
51 0 ▶sub trySetAttributes {
Subroutine trySetAttributes: not called CC=5 SCAR=34.0
Pod undocumented
52 0 my $self = shift;
53 0 ▶ my %attrs = ($#_ == 0) ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
$#_ == 0 ? : 0 0 0
54 0 my %unsetAttrs;
55 0 ▶ while (my ($attr,$value) = each %attrs) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %attrs) 0 0 0
56 0 ▶ if (UNIVERSAL::can($self, $attr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($self, $attr)) { } 0 0 0
57 0 eval {
58 0 $self->$attr($value);
59 };
60 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
61 0 Carp::carp("Couldn't set attribute $attr - $@");
62 0 $unsetAttrs{$attr} = $value;
63 }
64 }
65 else {
66 0 $unsetAttrs{$attr} = $value;
67 }
68 }
69 0 return \%unsetAttrs;
70 }
71  
72 9 ▶sub attributes {
Subroutine attributes: called CC=2 SCAR=6.9
Pod undocumented
73 9 my $self = shift;
74 9 foreach my $attr (@_) {
75 21 $self->attribute($attr);
76 }
77 }
78  
79 0 ▶sub methodAlias {
Subroutine methodAlias: not called CC=4 SCAR=23.2
Pod undocumented
80 0 my $self = shift;
81 0 my %aliases = @_;
82 0 ▶ my $class = ref($self) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
83 0 ▶ while (my ($alias,$existing) = each %aliases) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %aliases) 0 0 0
84 0 my $sub = UNIVERSAL::can($class, $existing);
85 0 ▶ if (!$sub) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sub) 0 0 0
86 0 Carp::croak("Method $existing does not exist.");
87 }
88 31 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
89 31 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
90 0 *{"${class}::${alias}"} = $sub;
91 }
92 }
93  
94 21 ▶sub attribute {
Subroutine attribute: called CC=3 SCAR=11.1
Pod undocumented
95 21 my $self = shift;
96 21 my $attr = shift;
97 21 ▶ my $attr_name = shift || $attr;
Condition33%1/3
shift() || $attr
l!l&&r!l&&!r
0210
MC/DC0%0/2
shift() || $attr
shift() $attr
Truth table33%1/3
A || B
A= shift()B= $attr
ABresult
1X1
011
000
98  
99 21 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
0210
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
100  
101 21 if (USE_QUALIFIED_ATTRIBUTE_NAMES) {
102 21 $attr_name = "${class}::${attr_name}";
103 }
104  
105 31 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
106 31 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
107 # Create a subroutine for an attribute getter/setter
108 21 ▶ *{"${class}::${attr}"} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
109 440 my $_self = shift;
110 440 ▶ if ($#_ == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ == 0) 59 381 440
111 59 $_self->{$attr_name} = $_[0];
112 }
113 440 return $_self->{$attr_name};
114 21 };
115 }
116  
117 0 ▶sub debug {
Subroutine debug: not called CC=12 SCAR=50.5
Pod undocumented
118 0 my $self = shift;
119 0 ▶ my $class = ref($self) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
120 0 ▶ if ($#_ >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ >= 0) 0 0 0
121 0 my $debug = shift;
122 0 ▶ if (UNIVERSAL::isa($debug, "CODE") or
Branch: 4 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($debug, 'CODE') or UNIVERSAL::isa($debug, 'GLOB') or UNIVERSAL::can($debug, 'debugMessage')) { } 0 0 0
elsif ($debug =~ /^STDERR$/i or $debug =~ /^(1|yes|true)$/i) { } 0 0 0
elsif ($debug =~ /^STDOUT$/i) { } 0 0 0
elsif ($debug and not $debug =~ /^(0|no|false)$/i) { } 0 0 0
Condition0%0/6
UNIVERSAL::isa($debug, "CODE") or UNIVERSAL::isa($debug, "GLOB") or UNIVERSAL::can($debug, "debugMessage")
l!l&&r!l&&!r
000
UNIVERSAL::isa($debug, "CODE") or UNIVERSAL::isa($debug, "GLOB")
l!l&&r!l&&!r
000
MC/DC0%0/3
UNIVERSAL::isa($debug, "CODE") or UNIVERSAL::isa($debug, "GLOB") or UNIVERSAL::can($debug, "debugMessage")
UNIVERSAL::isa($debug, "CODE") UNIVERSAL::isa($debug, "GLOB") UNIVERSAL::can($debug, "debugMessage")
Truth table0%0/4
A or B or C
A= UNIVERSAL::isa($debug, "CODE")B= UNIVERSAL::isa($debug, "GLOB")C= UNIVERSAL::can($debug, "debugMessage")
ABCresult
1XX1
01X1
0011
0000
123 UNIVERSAL::isa($debug, "GLOB") or
124 UNIVERSAL::can($debug, "debugMessage"))
125 {
126 0 $CLASS_DEBUG{$class} = $debug;
127 }
128 ▶ elsif ($debug =~ /^STDERR$/i or $debug =~ /^(1|yes|true)$/i) {
Condition0%0/3
$debug =~ /^STDERR$/i or $debug =~ /^(1|yes|true)$/i
l!l&&r!l&&!r
000
MC/DC0%0/2
$debug =~ /^STDERR$/i or $debug =~ /^(1|yes|true)$/i
$debug =~ /^STDERR$/i $debug =~ /^(1|yes|true)$/i
Truth table0%0/3
A or B
A= $debug =~ /^STDERR$/iB= $debug =~ /^(1|yes|true)$/i
ABresult
1X1
011
000
129 0 $CLASS_DEBUG{$class} = \*STDERR;
130 }
131 elsif ($debug =~ /^STDOUT$/i) {
132 0 $CLASS_DEBUG{$class} = \*STDOUT;
133 }
134 ▶ elsif ($debug and $debug !~ /^(0|no|false)$/i) { # true value indicating file
Condition0%0/3
$debug and not $debug =~ /^(0|no|false)$/i
!ll&&!rl&&r
000
MC/DC0%0/2
$debug and not $debug =~ /^(0|no|false)$/i
$debug not $debug =~ /^(0|no|false)$/i
Truth table0%0/3
A and not(B)
A= $debugB= not $debug =~ /^(0|no|false)$/i
ABresult
0X0
100
111
135 0 $CLASS_DEBUG{$class} = IO::File->new($debug, "a");
136 0 ▶ if (!$CLASS_DEBUG{$class}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $Net::Amazon::MechanicalTurk::BaseObject::CLASS_DEBUG{$class}) { } 0 0 0
137 0 print "Setting debug on $class to STDERR\n";
138 # Couldn't open so go to STDERR.
139 0 $CLASS_DEBUG{$class} = \*STDERR;
140 }
141 else {
142 0 $CLASS_DEBUG{$class}->autoflush(1);
143 }
144 }
145 else {
146 0 delete $CLASS_DEBUG{$class};
147 }
148 }
149 0 return $CLASS_DEBUG{$class};
150 }
151  
152 0 ▶sub debugMessage {
Subroutine debugMessage: not called CC=7 SCAR=40.3
Pod undocumented
153 0 my $self = shift;
154 0 my $debug = $self->debug;
155  
156 0 ▶ if (!defined($debug)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $debug) 0 0 0
157 0 return;
158 }
159  
160 0 my @stack = caller(1);
161 0 my @time = localtime(time());
162  
163 0 my $prefix = sprintf("[%04d-%02d-%02d %02d:%02d:%02d] %s >> ",
164 $time[5] + 1900,
165 $time[4] + 1,
166 $time[3],
167 $time[2],
168 $time[1],
169 $time[0],
170 $stack[3]
171 );
172  
173 0 my @messages = split(/\n/, join(" ", @_));
174 0 ▶ if (UNIVERSAL::isa($debug, "GLOB")) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($debug, 'GLOB')) { } 0 0 0
elsif (UNIVERSAL::isa($debug, 'CODE')) { } 0 0 0
175 0 foreach my $msg (@messages) {
176 0 print $debug $prefix.$msg."\n";
177 }
178 }
179 elsif (UNIVERSAL::isa($debug, "CODE")) {
180 0 foreach my $msg (@messages) {
181 0 $debug->($prefix.$msg."\n");
182 }
183 }
184 else {
185 0 foreach my $msg (@messages) {
186 0 $debug->debugMessage($prefix.$msg."\n");
187 }
188 }
189 }
190  
191 31 return 1;
« MechanicalTurk/Command/RetrieveResults.pm ↑ Summary MechanicalTurk/ModuleUtil.pm »