1 package Mouse::Meta::Method::Accessor;
6 # internal use only. do not call directly
7 sub generate_accessor_method_inline {
8 my ($class, $attribute) = @_;
10 my $name = $attribute->name;
11 my $default = $attribute->default;
12 my $constraint = $attribute->type_constraint;
13 my $builder = $attribute->builder;
14 my $trigger = $attribute->trigger;
15 my $is_weak = $attribute->is_weak_ref;
16 my $should_deref = $attribute->should_auto_deref;
17 my $should_coerce = $attribute->should_coerce;
19 my $compiled_type_constraint = $constraint ? $constraint->{_compiled_type_constraint} : undef;
22 my $key = $attribute->inlined_name;
25 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
27 if ($attribute->_is_metadata eq 'rw') {
29 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
30 'if (@_ >= 2) {' . "\n";
35 $accessor .= 'my $val = ';
39 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
40 'Mouse::Util::TypeConstraints->typecast_constraints("'.$attribute->associated_class->name.'", $attribute->{type_constraint}, '.$value.');';
42 $accessor .= $value.';';
44 if ($compiled_type_constraint) {
47 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
48 'unless ($compiled_type_constraint->($val)) {
49 $attribute->verify_type_constraint_error($name, $val, $attribute->{type_constraint});
54 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
55 'unless ($constraint->check($val)) {
56 $attribute->verify_type_constraint_error($name, $val, $attribute->{type_constraint});
62 # if there's nothing left to do for the attribute we can return during
64 $accessor .= 'return ' if !$is_weak && !$trigger && !$should_deref;
66 $accessor .= $self.'->{'.$key.'} = '.$value.';' . "\n";
69 $accessor .= 'Scalar::Util::weaken('.$self.'->{'.$key.'}) if ref('.$self.'->{'.$key.'});' . "\n";
73 $accessor .= '$trigger->('.$self.', '.$value.');' . "\n";
79 $accessor .= 'Carp::confess("Cannot assign a value to a read-only accessor") if scalar(@_) >= 2;' . "\n";
82 if ($attribute->is_lazy) {
83 $accessor .= $self.'->{'.$key.'} = ';
85 $accessor .= $attribute->has_builder
87 : ref($default) eq 'CODE'
88 ? '$default->('.$self.')'
90 $accessor .= ' if !exists '.$self.'->{'.$key.'};' . "\n";
94 my $type_constraint = $attribute->{type_constraint};
95 if (ref($type_constraint) && $type_constraint->name eq 'ArrayRef') {
96 $accessor .= 'if (wantarray) {
97 return @{ '.$self.'->{'.$key.'} || [] };
101 $accessor .= 'if (wantarray) {
102 return %{ '.$self.'->{'.$key.'} || {} };
107 $accessor .= 'return '.$self.'->{'.$key.'};
110 my $sub = eval $accessor;
111 Carp::confess($@) if $@;