1 package Moose::Meta::Method::Accessor::Native::Hash::set;
6 use List::MoreUtils ();
7 use Scalar::Util qw( looks_like_number );
11 with 'Moose::Meta::Method::Accessor::Native::Hash::Writer' => {
16 _inline_process_arguments
17 _inline_check_arguments
18 _inline_coerce_new_values
19 _inline_optimized_set_new_value
25 sub _minimum_arguments { 2 }
27 sub _maximum_arguments { undef }
29 around _inline_check_argument_count => sub {
36 $self->_inline_throw_error(
38 '"You must pass an even number of arguments to %s"',
39 $self->delegate_to_method,
46 sub _inline_process_arguments {
50 'my @keys_idx = grep { ! ($_ % 2) } 0..$#_;',
51 'my @values_idx = grep { $_ % 2 } 0..$#_;',
55 sub _inline_check_arguments {
60 'if (!defined($_[$_])) {',
61 $self->_inline_throw_error(
63 '"Hash keys passed to %s must be defined"',
64 $self->delegate_to_method,
72 sub _adds_members { 1 }
74 # We need to override this because while @_ can be written to, we cannot write
76 sub _inline_coerce_new_values {
79 return unless $self->associated_attribute->should_coerce;
81 return unless $self->_tc_member_type_can_coerce;
83 # Is there a simpler way to do this?
85 'my $iter = List::MoreUtils::natatime(2, @_);',
87 'while (my ($key, $val) = $iter->()) {',
88 'push @_, $key, $member_coercion->($val);',
93 sub _potential_value {
95 my ($slot_access) = @_;
97 return '{ %{ (' . $slot_access . ') }, @_ }';
100 sub _new_members { '@_[ @values_idx ]' }
102 sub _inline_optimized_set_new_value {
104 my ($inv, $new, $slot_access) = @_;
106 return '@{ (' . $slot_access . ') }{ @_[@keys_idx] } = @_[@values_idx];';
111 my ($slot_access) = @_;
114 . '? @{ (' . $slot_access . ') }{ @_[@keys_idx] } '
115 . ': ' . $slot_access . '->{ $_[$keys_idx[0]] }';