Fix issues on 5.6.2
[gitmo/Mouse.git] / lib / Mouse / Meta / Method / Accessor.pm
1 package Mouse::Meta::Method::Accessor;
2 use strict;
3 use warnings;
4 use Scalar::Util qw(blessed);
5
6 sub _generate_accessor{
7     my (undef, $attribute, $method_name, $class, $type) = @_;
8
9     my $name          = $attribute->name;
10     my $default       = $attribute->default;
11     my $constraint    = $attribute->type_constraint;
12     my $builder       = $attribute->builder;
13     my $trigger       = $attribute->trigger;
14     my $is_weak       = $attribute->is_weak_ref;
15     my $should_deref  = $attribute->should_auto_deref;
16     my $should_coerce = $attribute->should_coerce;
17
18     my $compiled_type_constraint = $constraint ? $constraint->_compiled_type_constraint : undef;
19
20     my $self  = '$_[0]';
21     my $key   = sprintf q{"%s"}, quotemeta $name;
22
23     $type ||= 'accessor';
24
25     my $accessor = 
26         '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
27         "sub {\n";
28
29     if ($type eq 'accessor' || $type eq 'writer') {
30         if($type eq 'accessor'){
31             $accessor .= 
32                 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
33                 'if (scalar(@_) >= 2) {' . "\n";
34         }
35         else{ # writer
36             $accessor .= 
37                 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
38                 'if(@_ < 2){ Carp::confess("Not enough arguments for the writer of '.$name.'") }'.
39                 '{' . "\n";
40         }
41                 
42         my $value = '$_[1]';
43
44         if (defined $constraint) {
45             if(!$compiled_type_constraint){
46                 Carp::confess("[BUG] Missing compiled type constraint for $constraint");
47             }
48             if ($should_coerce) {
49                 $accessor .=
50                     "\n".
51                     '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
52                     'my $val = $constraint->coerce('.$value.');';
53                 $value = '$val';
54             }
55             $accessor .= 
56                 "\n".
57                 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
58                 'unless ($compiled_type_constraint->('.$value.')) {
59                     $attribute->verify_type_constraint_error($name, '.$value.', $attribute->{type_constraint});
60                 }' . "\n";
61         }
62
63         # if there's nothing left to do for the attribute we can return during
64         # this setter
65         $accessor .= 'return ' if !$is_weak && !$trigger && !$should_deref;
66
67         $accessor .= $self.'->{'.$key.'} = '.$value.';' . "\n";
68
69         if ($is_weak) {
70             $accessor .= 'Scalar::Util::weaken('.$self.'->{'.$key.'}) if ref('.$self.'->{'.$key.'});' . "\n";
71         }
72
73         if ($trigger) {
74             $accessor .= '$trigger->('.$self.', '.$value.');' . "\n";
75         }
76
77         $accessor .= "}\n";
78     }
79     elsif($type eq 'reader') {
80         $accessor .= 'Carp::confess("Cannot assign a value to a read-only accessor") if scalar(@_) >= 2;' . "\n";
81     }
82     else{
83         $class->throw_error("Unknown accessor type '$type'");
84     }
85
86     if ($attribute->is_lazy) {
87         $accessor .= $self.'->{'.$key.'} = ';
88
89         if($should_coerce && defined($constraint)){
90             $accessor .= '$attribute->_coerce_and_verify(';
91         }
92         $accessor .=   $attribute->has_builder ? $self.'->$builder'
93                      : ref($default) eq 'CODE' ? '$default->('.$self.')'
94                      :                           '$default';
95
96         if($should_coerce && defined $constraint){
97             $accessor .= ')';
98         }
99         $accessor .= ' if !exists '.$self.'->{'.$key.'};' . "\n";
100     }
101
102     if ($should_deref) {
103         if ($constraint->is_a_type_of('ArrayRef')) {
104             $accessor .= 'if (wantarray) {
105                 return @{ '.$self.'->{'.$key.'} || [] };
106             }';
107         }
108         elsif($constraint->is_a_type_of('HashRef')){
109             $accessor .= 'if (wantarray) {
110                 return %{ '.$self.'->{'.$key.'} || {} };
111             }';
112         }
113         else{
114             $class->throw_error("Can not auto de-reference the type constraint " . $constraint->name);
115         }
116     }
117
118     $accessor .= 'return '.$self.'->{'.$key."};\n}";
119
120     #print $accessor, "\n";
121     my $code;
122     my $e = do{
123         local $@;
124         $code = eval $accessor;
125         $@;
126     };
127     die $e if $e;
128
129     if(defined $method_name){
130         $class->add_method($method_name => $code);
131     }
132
133     return $code;
134 }
135
136 sub _generate_reader{
137     my $class = shift;
138     return $class->_generate_accessor(@_, 'reader');
139 }
140
141 sub _generate_writer{
142     my $class = shift;
143     return $class->_generate_accessor(@_, 'writer');
144 }
145
146
147 sub _generate_predicate {
148     my (undef, $attribute, $method_name, $class) = @_;
149
150     my $slot = $attribute->name;
151
152     $class->add_method($method_name => sub{
153         return exists $_[0]->{$slot};
154     });
155     return;
156 }
157
158 sub _generate_clearer {
159     my (undef, $attribute, $method_name, $class) = @_;
160
161     my $slot = $attribute->name;
162
163     $class->add_method($method_name => sub{
164         delete $_[0]->{$slot};
165     });
166     return;
167 }
168
169 sub _generate_handles {
170     my (undef, $attribute, $handles, $class) = @_;
171
172     my $reader  = $attribute->reader || $attribute->accessor
173         or $class->throw_error("You must pass a reader method for '".$attribute->name."'");
174
175     my %handles = $attribute->_canonicalize_handles($handles);
176
177     foreach my $handle_name (keys %handles) {
178         my $method_to_call = $handles{$handle_name};
179
180         my $code = sub {
181             my $instance = shift;
182             my $proxy    = $instance->$reader();
183
184             my $error = !defined($proxy)                ? ' is not defined'
185                       : ref($proxy) && !blessed($proxy) ? qq{ is not an object (got '$proxy')}
186                                                         : undef;
187             if ($error) {
188                 $instance->meta->throw_error(
189                     "Cannot delegate $handle_name to $method_to_call because "
190                         . "the value of "
191                         . $attribute->name
192                         . $error
193                  );
194             }
195             $proxy->$method_to_call(@_);
196         };
197         $class->add_method($handle_name => $code);
198     }
199     return;
200 }
201
202
203 1;