1 package Moose::Meta::Method::Accessor::Native::Hash::set;
6 use List::MoreUtils ();
7 use Scalar::Util qw( looks_like_number );
10 $VERSION = eval $VERSION;
11 our $AUTHORITY = 'cpan:STEVAN';
15 with 'Moose::Meta::Method::Accessor::Native::Hash::Writer' => {
20 _inline_process_arguments
21 _inline_check_arguments
22 _inline_coerce_new_values
23 _inline_optimized_set_new_value
29 sub _minimum_arguments { 2 }
31 sub _maximum_arguments { undef }
33 around _inline_check_argument_count => sub {
40 $self->_inline_throw_error(
41 '"You must pass an even number of arguments to set"',
47 sub _inline_process_arguments {
51 'my @keys_idx = grep { ! ($_ % 2) } 0..$#_;',
52 'my @values_idx = grep { $_ % 2 } 0..$#_;',
56 sub _inline_check_arguments {
61 'if (!defined($_[$_])) {',
62 $self->_inline_throw_error(
63 '"Hash keys passed to set must be defined"',
70 sub _adds_members { 1 }
72 # We need to override this because while @_ can be written to, we cannot write
74 sub _inline_coerce_new_values {
77 return unless $self->associated_attribute->should_coerce;
79 return unless $self->_tc_member_type_can_coerce;
81 # Is there a simpler way to do this?
83 'my $iter = List::MoreUtils::natatime(2, @_);',
85 'while (my ($key, $val) = $iter->()) {',
86 'push @_, $key, $member_tc_obj->coerce($val);',
91 sub _potential_value {
93 my ($slot_access) = @_;
95 return '{ %{ (' . $slot_access . ') }, @_ }';
98 sub _new_members { '@_[ @values_idx ]' }
100 sub _inline_optimized_set_new_value {
102 my ($inv, $new, $slot_access) = @_;
104 return '@{ (' . $slot_access . ') }{ @_[@keys_idx] } = @_[@values_idx];';
109 my ($slot_access) = @_;
112 . '? @{ (' . $slot_access . ') }{ @_[@keys_idx] } '
113 . ': ' . $slot_access . '->{ $_[$keys_idx[0]] }';