1 package Mouse::Meta::Method::Accessor;
2 use Mouse::Util qw(:meta); # enables strict and warnings
4 use constant _MOUSE_DEBUG => !!$ENV{MOUSE_DEBUG};
7 my(undef, $self_var, $attr_name) = @_;
8 return sprintf '%s->{q{%s}}', $self_var, $attr_name;
11 sub _generate_accessor_any{
12 my($method_class, $type, $attribute, $class) = @_;
14 my $name = $attribute->name;
15 my $default = $attribute->default;
16 my $constraint = $attribute->type_constraint;
17 my $builder = $attribute->builder;
18 my $trigger = $attribute->trigger;
19 my $is_weak = $attribute->is_weak_ref;
20 my $should_deref = $attribute->should_auto_deref;
21 my $should_coerce = (defined($constraint) && $constraint->has_coercion && $attribute->should_coerce);
23 my $compiled_type_constraint = defined($constraint) ? $constraint->_compiled_type_constraint : undef;
26 my $slot = $method_class->_inline_slot($self, $name);;
28 my $accessor = sprintf(qq{package %s;\n#line 1 "%s-accessor for %s (%s)"\n}, $class->name, $type, $name, __FILE__)
31 if ($type eq 'rw' || $type eq 'wo') {
34 'if (scalar(@_) >= 2) {' . "\n";
38 'if(@_ < 2){ Carp::confess("Not enough arguments for the writer of $name") }'.
44 if (defined $constraint) {
48 'my $val = $constraint->coerce('.$value.');';
53 '$compiled_type_constraint->('.$value.') or
54 $attribute->_throw_type_constraint_error('.$value.', $constraint);' . "\n";
57 # if there's nothing left to do for the attribute we can return during
59 $accessor .= 'return ' if !$is_weak && !$trigger && !$should_deref;
61 $accessor .= "$slot = $value;\n";
64 $accessor .= "Scalar::Util::weaken($slot) if ref $slot;\n";
68 $accessor .= '$trigger->('.$self.', '.$value.');' . "\n";
73 elsif($type eq 'ro') {
74 $accessor .= 'Carp::confess("Cannot assign a value to a read-only accessor of $name") if scalar(@_) >= 2;' . "\n";
77 $class->throw_error("Unknown accessor type '$type'");
80 if ($attribute->is_lazy and $type ne 'wo') {
83 if (defined $builder){
84 $value = "$self->\$builder()";
86 elsif (ref($default) eq 'CODE'){
87 $value = "$self->\$default()";
93 $accessor .= "els" if $type eq 'rw';
94 $accessor .= "if(!exists $slot){\n";
96 $accessor .= "$slot = \$constraint->coerce($value)";
98 elsif(defined $constraint){
99 $accessor .= "my \$tmp = $value;\n";
100 $accessor .= "\$compiled_type_constraint->(\$tmp)";
101 $accessor .= " || \$attribute->_throw_type_constraint_error(\$tmp, \$constraint);\n";
102 $accessor .= "$slot = \$tmp;\n";
105 $accessor .= "$slot = $value;\n";
108 $accessor .= "Scalar::Util::weaken($slot) if ref $slot;\n";
114 if ($constraint->is_a_type_of('ArrayRef')) {
115 $accessor .= "return \@{ $slot || [] } if wantarray;\n";
117 elsif($constraint->is_a_type_of('HashRef')){
118 $accessor .= "return \%{ $slot || {} } if wantarray;\n";
121 $class->throw_error("Can not auto de-reference the type constraint " . $constraint->name);
125 $accessor .= "return $slot;\n}\n";
127 warn $accessor if _MOUSE_DEBUG;
131 $code = eval $accessor;
139 sub _generate_accessor{
140 #my($self, $attribute, $metaclass) = @_;
142 return $self->_generate_accessor_any(rw => @_);
145 sub _generate_reader {
146 #my($self, $attribute, $metaclass) = @_;
148 return $self->_generate_accessor_any(ro => @_);
151 sub _generate_writer {
152 #my($self, $attribute, $metaclass) = @_;
154 return $self->_generate_accessor_any(wo => @_);
157 sub _generate_predicate {
158 #my($self, $attribute, $metaclass) = @_;
159 my(undef, $attribute) = @_;
161 my $slot = $attribute->name;
163 return exists $_[0]->{$slot};
167 sub _generate_clearer {
168 #my($self, $attribute, $metaclass) = @_;
169 my(undef, $attribute) = @_;
171 my $slot = $attribute->name;
173 delete $_[0]->{$slot};
182 Mouse::Meta::Method::Accessor - A Mouse method generator for accessors
186 This document describes Mouse version 0.81
190 L<Moose::Meta::Method::Accessor>