1 package Moose::Meta::Method::Accessor::Native::Writer;
6 use List::MoreUtils qw( any );
10 with 'Moose::Meta::Method::Accessor::Native';
12 requires '_potential_value';
14 sub _generate_method {
18 my $slot_access = $self->_get_value($inv);
22 'my ' . $inv . ' = shift;',
23 $self->_inline_curried_arguments,
24 $self->_inline_writer_core($inv, $slot_access),
29 sub _inline_writer_core {
31 my ($inv, $slot_access) = @_;
33 my $potential = $self->_potential_value($slot_access);
38 $self->_inline_check_argument_count,
39 $self->_inline_process_arguments($inv, $slot_access),
40 $self->_inline_check_arguments('for writer'),
41 $self->_inline_check_lazy($inv, '$type_constraint', '$type_constraint_obj'),
44 if ($self->_return_value($slot_access)) {
45 # some writers will save the return value in this variable when they
46 # generate the potential value.
47 push @code, 'my @return;'
51 $self->_inline_coerce_new_values,
52 $self->_inline_copy_native_value(\$potential),
53 $self->_inline_tc_code($potential, '$type_constraint', '$type_constraint_obj'),
54 $self->_inline_get_old_value_for_trigger($inv, $old),
55 $self->_inline_capture_return_value($slot_access),
56 $self->_inline_set_new_value($inv, $potential, $slot_access),
57 $self->_inline_trigger($inv, $slot_access, $old),
58 $self->_inline_return_value($slot_access, 'for writer'),
64 sub _inline_process_arguments { return }
66 sub _inline_check_arguments { return }
68 sub _inline_coerce_new_values { return }
70 sub _writer_value_needs_copy {
73 return $self->_constraint_must_be_checked;
76 sub _constraint_must_be_checked {
79 my $attr = $self->associated_attribute;
81 return $attr->has_type_constraint
82 && (!$self->_is_root_type( $attr->type_constraint )
83 || ( $attr->should_coerce && $attr->type_constraint->has_coercion)
91 my $name = $type->name;
93 return any { $name eq $_ } @{ $self->root_types };
96 sub _inline_copy_native_value {
98 my ($potential_ref) = @_;
100 return unless $self->_writer_value_needs_copy;
102 my $code = 'my $potential = ' . ${$potential_ref} . ';';
104 ${$potential_ref} = '$potential';
109 around _inline_tc_code => sub {
112 my ($value, $tc, $tc_obj, $for_lazy) = @_;
114 return unless $for_lazy || $self->_constraint_must_be_checked;
116 return $self->$orig(@_);
119 sub _inline_check_coercion {
121 my ($value, $tc, $tc_obj) = @_;
123 my $attr = $self->associated_attribute;
124 return unless $attr->should_coerce && $attr->type_constraint->has_coercion;
126 # We want to break the aliasing in @_ in case the coercion tries to make a
127 # destructive change to an array member.
128 return $value . ' = ' . $tc_obj . '->coerce(' . $value . ');';
131 around _inline_check_constraint => sub {
134 my ($value, $tc, $tc_obj, $for_lazy) = @_;
136 return unless $for_lazy || $self->_constraint_must_be_checked;
138 return $self->$orig(@_);
141 sub _inline_capture_return_value { return }
143 sub _inline_set_new_value {
146 return $self->_inline_store_value(@_)
147 if $self->_writer_value_needs_copy
148 || !$self->_slot_access_can_be_inlined
149 || !$self->_get_is_lvalue;
151 return $self->_inline_optimized_set_new_value(@_);
157 return $self->associated_attribute->associated_class->instance_metaclass->inline_get_is_lvalue;
160 sub _inline_optimized_set_new_value {
163 return $self->_inline_store_value(@_);
168 my ($slot_access) = @_;