1 package Moose::Meta::Method::Accessor::Native::Hash::set;
6 use List::MoreUtils ();
7 use Scalar::Util qw( looks_like_number );
9 our $AUTHORITY = 'cpan:STEVAN';
13 with 'Moose::Meta::Method::Accessor::Native::Hash::Writer' => {
18 _inline_process_arguments
19 _inline_check_arguments
20 _inline_coerce_new_values
21 _inline_optimized_set_new_value
27 sub _minimum_arguments { 2 }
29 sub _maximum_arguments { undef }
31 around _inline_check_argument_count => sub {
38 $self->_inline_throw_error(
39 '"You must pass an even number of arguments to set"',
45 sub _inline_process_arguments {
49 'my @keys_idx = grep { ! ($_ % 2) } 0..$#_;',
50 'my @values_idx = grep { $_ % 2 } 0..$#_;',
54 sub _inline_check_arguments {
59 'if (!defined($_[$_])) {',
60 $self->_inline_throw_error(
61 '"Hash keys passed to set must be defined"',
68 sub _adds_members { 1 }
70 # We need to override this because while @_ can be written to, we cannot write
72 sub _inline_coerce_new_values {
75 return unless $self->associated_attribute->should_coerce;
77 return unless $self->_tc_member_type_can_coerce;
79 # Is there a simpler way to do this?
81 'my $iter = List::MoreUtils::natatime(2, @_);',
83 'while (my ($key, $val) = $iter->()) {',
84 'push @_, $key, $member_tc_obj->coerce($val);',
89 sub _potential_value {
91 my ($slot_access) = @_;
93 return '{ %{ (' . $slot_access . ') }, @_ }';
96 sub _new_members { '@_[ @values_idx ]' }
98 sub _inline_optimized_set_new_value {
100 my ($inv, $new, $slot_access) = @_;
102 return '@{ (' . $slot_access . ') }{ @_[@keys_idx] } = @_[@values_idx];';
107 my ($slot_access) = @_;
110 . '? @{ (' . $slot_access . ') }{ @_[@keys_idx] } '
111 . ': ' . $slot_access . '->{ $_[$keys_idx[0]] }';