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_optimized_set_new_value
28 sub _minimum_arguments { 2 }
30 sub _maximum_arguments { undef }
32 around _inline_check_argument_count => sub {
37 $self->$orig(@_) . "\n"
38 . $self->_inline_throw_error(
39 q{'You must pass an even number of arguments to set'})
43 sub _inline_process_arguments {
46 return 'my @keys_idx = grep { ! ($_ % 2) } 0..$#_;' . "\n"
47 . 'my @values_idx = grep { $_ % 2 } 0..$#_;';
50 sub _inline_check_arguments {
54 'for (@keys_idx) {' . "\n"
55 . $self->_inline_throw_error(
56 q{'Hash keys passed to set must be defined'})
57 . ' unless defined $_[$_];' . "\n" . '}';
60 sub _adds_members { 1 }
62 # We need to override this because while @_ can be written to, we cannot write
64 around _inline_coerce_new_values => sub {
68 return q{} unless $self->associated_attribute->should_coerce;
70 return q{} unless $self->_tc_member_type_can_coerce;
72 # Is there a simpler way to do this?
73 return 'my $iter = List::MoreUtils::natatime 2, @_;'
75 . 'while ( my ( $key, $val ) = $iter->() ) {'
76 . 'push @_, $key, $member_tc_obj->coerce($val);'
80 sub _potential_value {
81 my ( $self, $slot_access ) = @_;
83 return "{ %{ ($slot_access) }, \@_ }";
86 sub _new_members { '@_[ @values_idx ]' }
88 sub _inline_optimized_set_new_value {
89 my ( $self, $inv, $new, $slot_access ) = @_;
91 return "\@{ ($slot_access) }{ \@_[ \@keys_idx] } = \@_[ \@values_idx ]";
95 my ( $self, $slot_access ) = @_;
97 return "return wantarray ? \@{ ($slot_access) }{ \@_[ \@keys_idx ] } : ${slot_access}->{ \$_[ \$keys_idx[0] ] };";