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 (scalar(@_) >= 2) {' . "\n";
38 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
39 'my $val = Mouse::Util::TypeConstraints->typecast_constraints("'.$attribute->associated_class->name.'", $attribute->{type_constraint}, '.$value.');';
42 if ($compiled_type_constraint) {
45 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
46 'unless ($compiled_type_constraint->('.$value.')) {
47 $attribute->verify_type_constraint_error($name, '.$value.', $attribute->{type_constraint});
52 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
53 'unless ($constraint->check('.$value.')) {
54 $attribute->verify_type_constraint_error($name, '.$value.', $attribute->{type_constraint});
59 # if there's nothing left to do for the attribute we can return during
61 $accessor .= 'return ' if !$is_weak && !$trigger && !$should_deref;
63 $accessor .= $self.'->{'.$key.'} = '.$value.';' . "\n";
66 $accessor .= 'Scalar::Util::weaken('.$self.'->{'.$key.'}) if ref('.$self.'->{'.$key.'});' . "\n";
70 $accessor .= '$trigger->('.$self.', '.$value.');' . "\n";
76 $accessor .= 'Carp::confess("Cannot assign a value to a read-only accessor") if scalar(@_) >= 2;' . "\n";
79 if ($attribute->is_lazy) {
80 $accessor .= $self.'->{'.$key.'} = ';
82 $accessor .= $attribute->has_builder
84 : ref($default) eq 'CODE'
85 ? '$default->('.$self.')'
87 $accessor .= ' if !exists '.$self.'->{'.$key.'};' . "\n";
91 if (ref($constraint) && $constraint->name eq 'ArrayRef') {
92 $accessor .= 'if (wantarray) {
93 return @{ '.$self.'->{'.$key.'} || [] };
97 $accessor .= 'if (wantarray) {
98 return %{ '.$self.'->{'.$key.'} || {} };
103 $accessor .= 'return '.$self.'->{'.$key.'};
106 my $sub = eval $accessor;
107 Carp::confess($@) if $@;