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';
13 sub _minimum_arguments { 2 }
15 sub _maximum_arguments { undef }
17 around _inline_check_argument_count => sub {
24 $self->_inline_throw_error(
26 '"You must pass an even number of arguments to %s"',
27 $self->delegate_to_method,
34 sub _inline_process_arguments {
38 'my @keys_idx = grep { ! ($_ % 2) } 0..$#_;',
39 'my @values_idx = grep { $_ % 2 } 0..$#_;',
43 sub _inline_check_arguments {
48 'if (!defined($_[$_])) {',
49 $self->_inline_throw_error(
51 '"Hash keys passed to %s must be defined"',
52 $self->delegate_to_method,
60 sub _adds_members { 1 }
62 # We need to override this because while @_ can be written to, we cannot write
64 sub _inline_coerce_new_values {
67 return unless $self->associated_attribute->should_coerce;
69 return unless $self->_tc_member_type_can_coerce;
71 # Is there a simpler way to do this?
73 'my $iter = List::MoreUtils::natatime(2, @_);',
75 'while (my ($key, $val) = $iter->()) {',
76 'push @_, $key, $member_coercion->($val);',
81 sub _potential_value {
83 my ($slot_access) = @_;
85 return '{ %{ (' . $slot_access . ') }, @_ }';
88 sub _new_members { '@_[ @values_idx ]' }
90 sub _inline_optimized_set_new_value {
92 my ($inv, $new, $slot_access) = @_;
94 return '@{ (' . $slot_access . ') }{ @_[@keys_idx] } = @_[@values_idx];';
99 my ($slot_access) = @_;
102 . '? @{ (' . $slot_access . ') }{ @_[@keys_idx] } '
103 . ': ' . $slot_access . '->{ $_[$keys_idx[0]] }';