2 package Moose::Meta::Method::Accessor;
7 our $VERSION = '0.62_01';
8 $VERSION = eval $VERSION;
9 our $AUTHORITY = 'cpan:STEVAN';
11 use base 'Moose::Meta::Method',
12 'Class::MOP::Method::Accessor';
16 ( ref $self && $self->associated_attribute ) || $self->SUPER::_error_thrower();
20 my ( $self, $code ) = @_;
23 # set up the environment
25 my $attr = $self->associated_attribute;
26 my $attr_name = $attr->name;
29 my $type_constraint_obj = $attr->type_constraint;
30 my $type_constraint_name = $type_constraint_obj && $type_constraint_obj->name;
31 my $type_constraint = $type_constraint_obj
32 ? $type_constraint_obj->_compiled_type_constraint
36 #warn "code for $attr_name =>\n" . $code . "\n";
37 my $sub = $self->_eval_closure($environment, $code);
38 $self->throw_error("Could not create writer for '${\$self->associated_attribute->name}' because $@ \n code: $code", error => $@, data => $code ) if $@;
43 sub generate_accessor_method_inline {
45 my $attr = $self->associated_attribute;
46 my $attr_name = $attr->name;
48 my $slot_access = $self->_inline_access($inv, $attr_name);
49 my $value_name = $self->_value_needs_copy ? '$val' : '$_[1]';
51 $self->_eval_code('sub { ' . "\n"
52 . $self->_inline_pre_body(@_) . "\n"
53 . 'if (scalar(@_) >= 2) {' . "\n"
54 . $self->_inline_copy_value . "\n"
55 . $self->_inline_check_required . "\n"
56 . $self->_inline_check_coercion . "\n"
57 . $self->_inline_check_constraint($value_name) . "\n"
58 . $self->_inline_store($inv, $value_name) . "\n"
59 . $self->_inline_trigger($inv, $value_name) . "\n"
61 . $self->_inline_check_lazy($inv) . "\n"
62 . $self->_inline_post_body(@_) . "\n"
63 . 'return ' . $self->_inline_auto_deref($self->_inline_get($inv)) . "\n"
67 sub generate_writer_method_inline {
69 my $attr = $self->associated_attribute;
70 my $attr_name = $attr->name;
72 my $slot_access = $self->_inline_get($inv, $attr_name);
73 my $value_name = $self->_value_needs_copy ? '$val' : '$_[1]';
75 $self->_eval_code('sub { '
76 . $self->_inline_pre_body(@_)
77 . $self->_inline_copy_value
78 . $self->_inline_check_required
79 . $self->_inline_check_coercion
80 . $self->_inline_check_constraint($value_name)
81 . $self->_inline_store($inv, $value_name)
82 . $self->_inline_post_body(@_)
83 . $self->_inline_trigger($inv, $value_name)
87 sub generate_reader_method_inline {
89 my $attr = $self->associated_attribute;
90 my $attr_name = $attr->name;
92 my $slot_access = $self->_inline_get($inv, $attr_name);
94 $self->_eval_code('sub {'
95 . $self->_inline_pre_body(@_)
96 . $self->_inline_throw_error('"Cannot assign a value to a read-only accessor"', 'data => \@_') . ' if @_ > 1;'
97 . $self->_inline_check_lazy($inv)
98 . $self->_inline_post_body(@_)
99 . 'return ' . $self->_inline_auto_deref( $slot_access ) . ';'
103 sub _inline_copy_value {
104 return '' unless shift->_value_needs_copy;
105 return 'my $val = $_[1];'
108 sub _value_needs_copy {
109 my $attr = (shift)->associated_attribute;
110 return $attr->should_coerce;
113 sub generate_reader_method { shift->generate_reader_method_inline(@_) }
114 sub generate_writer_method { shift->generate_writer_method_inline(@_) }
115 sub generate_accessor_method { shift->generate_accessor_method_inline(@_) }
117 sub _inline_pre_body { '' }
118 sub _inline_post_body { '' }
120 sub _inline_check_constraint {
121 my ($self, $value) = @_;
123 my $attr = $self->associated_attribute;
124 my $attr_name = $attr->name;
126 return '' unless $attr->has_type_constraint;
128 my $type_constraint_name = $attr->type_constraint->name;
130 qq{\$type_constraint->($value) || } . $self->_inline_throw_error(qq{"Attribute ($attr_name) does not pass the type constraint because: " . \$type_constraint_obj->get_message($value)}, "data => $value") . ";";
133 sub _inline_check_coercion {
134 my $attr = (shift)->associated_attribute;
136 return '' unless $attr->should_coerce;
137 return '$val = $attr->type_constraint->coerce($_[1]);'
140 sub _inline_check_required {
142 my $attr = $self->associated_attribute;
144 my $attr_name = $attr->name;
146 return '' unless $attr->is_required;
147 return qq{(\@_ >= 2) || } . $self->_inline_throw_error(qq{"Attribute ($attr_name) is required, so cannot be set to undef"}) . ';' # defined $_[1] is not good enough
150 sub _inline_check_lazy {
151 my ($self, $instance) = @_;
153 my $attr = $self->associated_attribute;
155 return '' unless $attr->is_lazy;
157 my $slot_access = $self->_inline_access($instance, $attr->name);
159 my $slot_exists = $self->_inline_has($instance, $attr->name);
161 my $code = 'unless (' . $slot_exists . ') {' . "\n";
162 if ($attr->has_type_constraint) {
163 if ($attr->has_default || $attr->has_builder) {
164 if ($attr->has_default) {
165 $code .= ' my $default = $attr->default(' . $instance . ');'."\n";
167 elsif ($attr->has_builder) {
168 $code .= ' my $default;'."\n".
169 ' if(my $builder = '.$instance.'->can($attr->builder)){ '."\n".
170 ' $default = '.$instance.'->$builder; '. "\n } else {\n" .
171 ' ' . $self->_inline_throw_error(q{sprintf "%s does not support builder method '%s' for attribute '%s'", ref(} . $instance . ') || '.$instance.', $attr->builder, $attr->name') .
174 $code .= ' $default = $type_constraint_obj->coerce($default);'."\n" if $attr->should_coerce;
175 $code .= ' ($type_constraint->($default))' .
176 ' || ' . $self->_inline_throw_error('"Attribute (" . $attr_name . ") does not pass the type constraint ("' .
177 ' . $type_constraint_name . ") with " . (defined($default) ? overload::StrVal($default) : "undef")' ) . ';'
179 $code .= ' ' . $self->_inline_init_slot($attr, $instance, $slot_access, '$default') . "\n";
182 $code .= ' ' . $self->_inline_init_slot($attr, $instance, $slot_access, 'undef') . "\n";
186 if ($attr->has_default) {
187 $code .= ' ' . $self->_inline_init_slot($attr, $instance, $slot_access, ('$attr->default(' . $instance . ')')) . "\n";
189 elsif ($attr->has_builder) {
190 $code .= ' if (my $builder = '.$instance.'->can($attr->builder)) { ' . "\n"
191 . ' ' . $self->_inline_init_slot($attr, $instance, $slot_access, ($instance . '->$builder'))
193 . ' ' . $self->_inline_throw_error(q{sprintf "%s does not support builder method '%s' for attribute '%s'", ref(} . $instance . ') || '.$instance.', $attr->builder, $attr->name')
197 $code .= ' ' . $self->_inline_init_slot($attr, $instance, $slot_access, 'undef') . "\n";
204 sub _inline_init_slot {
205 my ($self, $attr, $inv, $slot_access, $value) = @_;
206 if ($attr->has_initializer) {
207 return ('$attr->set_initial_value(' . $inv . ', ' . $value . ');');
210 return ($slot_access . ' = ' . $value . ';');
215 my ($self, $instance, $value) = @_;
216 my $attr = $self->associated_attribute;
218 my $mi = $attr->associated_class->get_meta_instance;
219 my $slot_name = sprintf "'%s'", $attr->slots;
221 my $code = $mi->inline_set_slot_value($instance, $slot_name, $value) . ";";
222 $code .= $mi->inline_weaken_slot_value($instance, $slot_name, $value) . ";"
223 if $attr->is_weak_ref;
227 sub _inline_trigger {
228 my ($self, $instance, $value) = @_;
229 my $attr = $self->associated_attribute;
230 return '' unless $attr->has_trigger;
231 return sprintf('$attr->trigger->(%s, %s);', $instance, $value);
235 my ($self, $instance) = @_;
236 my $attr = $self->associated_attribute;
238 my $mi = $attr->associated_class->get_meta_instance;
239 my $slot_name = sprintf "'%s'", $attr->slots;
241 return $mi->inline_get_slot_value($instance, $slot_name);
245 my ($self, $instance) = @_;
246 my $attr = $self->associated_attribute;
248 my $mi = $attr->associated_class->get_meta_instance;
249 my $slot_name = sprintf "'%s'", $attr->slots;
251 return $mi->inline_slot_access($instance, $slot_name);
255 my ($self, $instance) = @_;
256 my $attr = $self->associated_attribute;
258 my $mi = $attr->associated_class->get_meta_instance;
259 my $slot_name = sprintf "'%s'", $attr->slots;
261 return $mi->inline_is_slot_initialized($instance, $slot_name);
264 sub _inline_auto_deref {
265 my ( $self, $ref_value ) = @_;
266 my $attr = $self->associated_attribute;
268 return $ref_value unless $attr->should_auto_deref;
270 my $type_constraint = $attr->type_constraint;
273 if ($type_constraint->is_a_type_of('ArrayRef')) {
276 elsif ($type_constraint->is_a_type_of('HashRef')) {
280 $self->throw_error("Can not auto de-reference the type constraint '" . $type_constraint->name . "'", type_constraint => $type_constraint );
283 "(wantarray() ? $sigil\{ ( $ref_value ) || return } : ( $ref_value ) )";
294 Moose::Meta::Method::Accessor - A Moose Method metaclass for accessors
298 This is a subclass of L<Class::MOP::Method::Accessor> and it's primary
299 responsibility is to generate the accessor methods for attributes. It
300 can handle both closure based accessors, as well as inlined source based
303 This is a fairly new addition to the MOP, but this will play an important
304 role in the optimization strategy we are currently following.
310 =item B<generate_accessor_method>
312 =item B<generate_reader_method>
314 =item B<generate_writer_method>
316 =item B<generate_accessor_method_inline>
318 =item B<generate_reader_method_inline>
320 =item B<generate_writer_method_inline>
326 All complex software has bugs lurking in it, and this module is no
327 exception. If you find a bug please either email me, or add the bug
332 Stevan Little E<lt>stevan@iinteractive.comE<gt>
334 Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
336 =head1 COPYRIGHT AND LICENSE
338 Copyright 2006-2008 by Infinity Interactive, Inc.
340 L<http://www.iinteractive.com>
342 This library is free software; you can redistribute it and/or modify
343 it under the same terms as Perl itself.