1 package Moose::Meta::Method::Accessor::Native;
6 use Carp qw( confess );
7 use Scalar::Util qw( blessed weaken );
9 our $VERSION = '1.9900';
10 $VERSION = eval $VERSION;
11 our $AUTHORITY = 'cpan:STEVAN';
20 $options{curried_arguments} = []
21 unless exists $options{curried_arguments};
23 confess 'You must supply a curried_arguments which is an ARRAY reference'
24 unless $options{curried_arguments}
25 && ref($options{curried_arguments}) eq 'ARRAY';
27 $options{definition_context} = $options{attribute}->definition_context;
29 $options{accessor_type} = 'native';
31 return $class->$orig(%options);
36 my $options = @_ == 1 ? $_[0] : {@_};
38 return bless $options, $class;
41 sub root_types { (shift)->{'root_types'} }
43 sub _initialize_body {
46 $self->{'body'} = $self->_compile_code( [$self->_generate_method] );
51 sub _inline_curried_arguments {
54 return unless @{ $self->curried_arguments };
56 return 'unshift @_, @curried;';
59 sub _inline_check_argument_count {
64 if (my $min = $self->_minimum_arguments) {
66 'if (@_ < ' . $min . ') {',
67 $self->_inline_throw_error(
69 '"Cannot call %s without at least %s argument%s"',
70 $self->delegate_to_method,
72 ($min == 1 ? '' : 's'),
79 if (defined(my $max = $self->_maximum_arguments)) {
81 'if (@_ > ' . $max . ') {',
82 $self->_inline_throw_error(
84 '"Cannot call %s with %s argument%s"',
85 $self->delegate_to_method,
86 $max ? "more than $max" : 'any',
87 ($max == 1 ? '' : 's'),
97 sub _inline_return_value {
99 my ($slot_access, $for_writer) = @_;
101 return 'return ' . $self->_return_value($slot_access, $for_writer) . ';';
104 sub _minimum_arguments { 0 }
105 sub _maximum_arguments { undef }
107 override _get_value => sub {
111 return $self->_slot_access_can_be_inlined
113 : $instance . '->$reader';
116 override _inline_store_value => sub {
118 my ($instance, $value) = @_;
120 return $self->_slot_access_can_be_inlined
122 : $instance . '->$writer(' . $value . ');';
125 override _eval_environment => sub {
130 $env->{'@curried'} = $self->curried_arguments;
132 return $env if $self->_slot_access_can_be_inlined;
134 my $reader = $self->associated_attribute->get_read_method_ref;
135 $reader = $reader->body if blessed $reader;
137 $env->{'$reader'} = \$reader;
139 my $writer = $self->associated_attribute->get_write_method_ref;
140 $writer = $writer->body if blessed $writer;
142 $env->{'$writer'} = \$writer;
147 sub _slot_access_can_be_inlined {
150 return $self->is_inline && $self->_instance_is_inlinable;