| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Net::Amazon::MechanicalTurk::BaseObject; | 
| 2 | 30 |  |  | 30 |  | 416 | use strict; | 
|  | 30 |  |  |  |  | 114 |  | 
|  | 30 |  |  |  |  | 985 |  | 
| 3 | 30 |  |  | 30 |  | 140 | use warnings; | 
|  | 30 |  |  |  |  | 85 |  | 
|  | 30 |  |  |  |  | 706 |  | 
| 4 | 30 |  |  | 30 |  | 137 | use Carp; | 
|  | 30 |  |  |  |  | 50 |  | 
|  | 30 |  |  |  |  | 1794 |  | 
| 5 | 30 |  |  | 30 |  | 25863 | use IO::File; | 
|  | 30 |  |  |  |  | 323891 |  | 
|  | 30 |  |  |  |  | 5520 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | our $VERSION = '1.00'; | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 30 |  |  | 30 |  | 285 | use constant USE_QUALIFIED_ATTRIBUTE_NAMES => 1; | 
|  | 30 |  |  |  |  | 73 |  | 
|  | 30 |  |  |  |  | 22635 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | our %CLASS_DEBUG; | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | sub new { | 
| 14 | 31 |  |  | 31 | 0 | 6833 | my $class = shift; | 
| 15 | 31 |  |  |  |  | 115 | my $self = bless {}, $class; | 
| 16 | 31 |  |  |  |  | 233 | $self->init(@_); | 
| 17 | 11 |  |  |  |  | 55 | return $self; | 
| 18 |  |  |  |  |  |  | } | 
| 19 |  |  |  |  |  |  |  | 
| 20 | 0 |  |  | 0 | 0 | 0 | sub init {} | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 0 |  |  | 0 |  | 0 | sub DESTROY {} | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | sub assertRequiredAttributes { | 
| 25 | 9 |  |  | 9 | 0 | 18 | my $self = shift; | 
| 26 | 9 |  |  |  |  | 21 | foreach my $attr (@_) { | 
| 27 | 11 | 50 |  |  |  | 29 | if (!defined($self->$attr)) { | 
| 28 | 0 |  |  |  |  | 0 | Carp::croak("Required attribute ${attr} was not set."); | 
| 29 |  |  |  |  |  |  | } | 
| 30 |  |  |  |  |  |  | } | 
| 31 |  |  |  |  |  |  | } | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | sub setAttributesIfNotDefined { | 
| 34 | 7 |  |  | 7 | 0 | 13 | my $self = shift; | 
| 35 | 7 | 50 |  |  |  | 45 | my %attrs = ($#_ == 0) ? %{$_[0]} : @_; | 
|  | 0 |  |  |  |  | 0 |  | 
| 36 | 7 |  |  |  |  | 36 | while (my ($attr,$value) = each %attrs) { | 
| 37 | 25 | 100 |  |  |  | 26 | eval { $self->$attr($value) unless defined($self->$attr); }; | 
|  | 25 |  |  |  |  | 66 |  | 
| 38 | 25 | 50 |  |  |  | 117 | if ($@) { Carp::croak("Can't set attribute $attr - $@"); } | 
|  | 0 |  |  |  |  | 0 |  | 
| 39 |  |  |  |  |  |  | } | 
| 40 |  |  |  |  |  |  | } | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | sub setAttributes { | 
| 43 | 29 |  |  | 29 | 0 | 67 | my $self = shift; | 
| 44 | 29 | 50 |  |  |  | 190 | my %attrs = ($#_ == 0) ? %{$_[0]} : @_; | 
|  | 0 |  |  |  |  | 0 |  | 
| 45 | 29 |  |  |  |  | 234 | while (my ($attr,$value) = each %attrs) { | 
| 46 | 18 |  |  |  |  | 28 | eval { $self->$attr($value); }; | 
|  | 18 |  |  |  |  | 61 |  | 
| 47 | 18 | 50 |  |  |  | 118 | if ($@) { Carp::croak("Can't set attribute $attr - $@"); } | 
|  | 0 |  |  |  |  | 0 |  | 
| 48 |  |  |  |  |  |  | } | 
| 49 |  |  |  |  |  |  | } | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | sub trySetAttributes { | 
| 52 | 0 |  |  | 0 | 0 | 0 | my $self = shift; | 
| 53 | 0 | 0 |  |  |  | 0 | my %attrs = ($#_ == 0) ? %{$_[0]} : @_; | 
|  | 0 |  |  |  |  | 0 |  | 
| 54 | 0 |  |  |  |  | 0 | my %unsetAttrs; | 
| 55 | 0 |  |  |  |  | 0 | while (my ($attr,$value) = each %attrs) { | 
| 56 | 0 | 0 |  |  |  | 0 | if (UNIVERSAL::can($self, $attr)) { | 
| 57 | 0 |  |  |  |  | 0 | eval { | 
| 58 | 0 |  |  |  |  | 0 | $self->$attr($value); | 
| 59 |  |  |  |  |  |  | }; | 
| 60 | 0 | 0 |  |  |  | 0 | if ($@) { | 
| 61 | 0 |  |  |  |  | 0 | Carp::carp("Couldn't set attribute $attr - $@"); | 
| 62 | 0 |  |  |  |  | 0 | $unsetAttrs{$attr} = $value; | 
| 63 |  |  |  |  |  |  | } | 
| 64 |  |  |  |  |  |  | } | 
| 65 |  |  |  |  |  |  | else { | 
| 66 | 0 |  |  |  |  | 0 | $unsetAttrs{$attr} = $value; | 
| 67 |  |  |  |  |  |  | } | 
| 68 |  |  |  |  |  |  | } | 
| 69 | 0 |  |  |  |  | 0 | return \%unsetAttrs; | 
| 70 |  |  |  |  |  |  | } | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | sub attributes { | 
| 73 | 116 |  |  | 116 | 0 | 300 | my $self = shift; | 
| 74 | 116 |  |  |  |  | 319 | foreach my $attr (@_) { | 
| 75 | 523 |  |  |  |  | 1874 | $self->attribute($attr); | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | sub methodAlias { | 
| 80 | 18 |  |  | 18 | 0 | 56 | my $self = shift; | 
| 81 | 18 |  |  |  |  | 173 | my %aliases = @_; | 
| 82 | 18 |  | 33 |  |  | 135 | my $class = ref($self) || $self; | 
| 83 | 18 |  |  |  |  | 191 | while (my ($alias,$existing) = each %aliases) { | 
| 84 | 162 |  |  |  |  | 590 | my $sub = UNIVERSAL::can($class, $existing); | 
| 85 | 162 | 50 |  |  |  | 309 | if (!$sub) { | 
| 86 | 0 |  |  |  |  | 0 | Carp::croak("Method $existing does not exist."); | 
| 87 |  |  |  |  |  |  | } | 
| 88 | 30 |  |  | 30 |  | 185 | no strict 'refs'; | 
|  | 30 |  |  |  |  | 58 |  | 
|  | 30 |  |  |  |  | 949 |  | 
| 89 | 30 |  |  | 30 |  | 154 | no warnings; | 
|  | 30 |  |  |  |  | 50 |  | 
|  | 30 |  |  |  |  | 5198 |  | 
| 90 | 162 |  |  |  |  | 172 | *{"${class}::${alias}"} = $sub; | 
|  | 162 |  |  |  |  | 1161 |  | 
| 91 |  |  |  |  |  |  | } | 
| 92 |  |  |  |  |  |  | } | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | sub attribute { | 
| 95 | 523 |  |  | 523 | 0 | 644 | my $self = shift; | 
| 96 | 523 |  |  |  |  | 592 | my $attr = shift; | 
| 97 | 523 |  | 33 |  |  | 2049 | my $attr_name = shift || $attr; | 
| 98 |  |  |  |  |  |  |  | 
| 99 | 523 |  | 33 |  |  | 1690 | my $class = ref($self) || $self; | 
| 100 |  |  |  |  |  |  |  | 
| 101 | 523 |  |  |  |  | 521 | if (USE_QUALIFIED_ATTRIBUTE_NAMES) { | 
| 102 | 523 |  |  |  |  | 1055 | $attr_name = "${class}::${attr_name}"; | 
| 103 |  |  |  |  |  |  | } | 
| 104 |  |  |  |  |  |  |  | 
| 105 | 30 |  |  | 30 |  | 328 | no strict 'refs'; | 
|  | 30 |  |  |  |  | 66 |  | 
|  | 30 |  |  |  |  | 1332 |  | 
| 106 | 30 |  |  | 30 |  | 157 | no warnings; | 
|  | 30 |  |  |  |  | 81 |  | 
|  | 30 |  |  |  |  | 26155 |  | 
| 107 |  |  |  |  |  |  | # Create a subroutine for an attribute getter/setter | 
| 108 | 523 |  |  |  |  | 3429 | *{"${class}::${attr}"} = sub { | 
| 109 | 641 |  |  | 641 |  | 1244 | my $_self = shift; | 
| 110 | 641 | 100 |  |  |  | 1332 | if ($#_ == 0) { | 
| 111 | 83 |  |  |  |  | 623 | $_self->{$attr_name} = $_[0]; | 
| 112 |  |  |  |  |  |  | } | 
| 113 | 641 |  |  |  |  | 2730 | return $_self->{$attr_name}; | 
| 114 | 523 |  |  |  |  | 1675 | }; | 
| 115 |  |  |  |  |  |  | } | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | sub debug { | 
| 118 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 119 | 0 |  | 0 |  |  |  | my $class = ref($self) || $self; | 
| 120 | 0 | 0 |  |  |  |  | if ($#_ >= 0) { | 
| 121 | 0 |  |  |  |  |  | my $debug = shift; | 
| 122 | 0 | 0 | 0 |  |  |  | if (UNIVERSAL::isa($debug, "CODE") or | 
|  |  | 0 | 0 |  |  |  |  | 
|  |  | 0 | 0 |  |  |  |  | 
|  |  | 0 | 0 |  |  |  |  | 
| 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) { | 
| 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 | 
| 135 | 0 |  |  |  |  |  | $CLASS_DEBUG{$class} = IO::File->new($debug, "a"); | 
| 136 | 0 | 0 |  |  |  |  | if (!$CLASS_DEBUG{$class}) { | 
| 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 |  |  |  |  |  |  | sub debugMessage { | 
| 153 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 154 | 0 |  |  |  |  |  | my $debug = $self->debug; | 
| 155 |  |  |  |  |  |  |  | 
| 156 | 0 | 0 |  |  |  |  | if (!defined($debug)) { | 
| 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 | 0 |  |  |  |  | if (UNIVERSAL::isa($debug, "GLOB")) { | 
|  |  | 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 |  |  |  |  |  |  | return 1; |