1 package Moose::Meta::Method::Accessor::Native;
6 use Carp qw( confess );
7 use Scalar::Util qw( blessed weaken );
10 $VERSION = eval $VERSION;
11 our $AUTHORITY = 'cpan:STEVAN';
20 exists $options{curried_arguments}
21 || ( $options{curried_arguments} = [] );
23 ( $options{curried_arguments}
24 && ( 'ARRAY' eq ref $options{curried_arguments} ) )
26 'You must supply a curried_arguments which is an ARRAY reference';
28 $options{definition_context} = $options{attribute}->definition_context;
30 $options{accessor_type} = 'native';
32 return $class->$orig(%options);
38 my $options = @_ == 1 ? $_[0] : {@_};
40 return bless $options, $class;
43 sub root_types { (shift)->{'root_types'} }
45 sub _initialize_body {
48 $self->{'body'} = $self->_eval_code( $self->_generate_method );
53 sub _inline_curried_arguments {
56 return q{} unless @{ $self->curried_arguments };
58 return 'unshift @_, @curried;'
61 sub _inline_check_argument_count {
66 if ( my $min = $self->_minimum_arguments ) {
67 my $err_msg = sprintf(
68 q{"Cannot call %s without at least %s argument%s"},
69 $self->delegate_to_method,
71 ( $min == 1 ? q{} : 's' )
76 . $self->_inline_throw_error($err_msg)
77 . " unless \@_ >= $min;";
80 if ( defined( my $max = $self->_maximum_arguments ) ) {
81 my $err_msg = sprintf(
82 q{"Cannot call %s with %s argument%s"},
83 $self->delegate_to_method,
84 ( $max ? "more than $max" : 'any' ),
85 ( $max == 1 ? q{} : 's' )
90 . $self->_inline_throw_error($err_msg)
97 sub _minimum_arguments { 0 }
98 sub _maximum_arguments { undef }
100 override _inline_get => sub {
101 my ( $self, $instance ) = @_;
103 return $self->_slot_access_can_be_inlined
105 : "${instance}->\$reader";
108 override _inline_store => sub {
109 my ( $self, $instance, $value ) = @_;
111 return $self->_slot_access_can_be_inlined
113 : "${instance}->\$writer($value)";
116 override _eval_environment => sub {
121 $env->{'@curried'} = $self->curried_arguments;
123 return $env if $self->_slot_access_can_be_inlined;
125 my $reader = $self->associated_attribute->get_read_method_ref;
126 $reader = $reader->body if blessed $reader;
128 $env->{'$reader'} = \$reader;
130 my $writer = $self->associated_attribute->get_write_method_ref;
131 $writer = $writer->body if blessed $writer;
133 $env->{'$writer'} = \$writer;
138 sub _slot_access_can_be_inlined {
141 return $self->is_inline && $self->_instance_is_inlinable;