1 package Mouse::Meta::Method::Accessor;
2 use Mouse::Util qw(:meta); # enables strict and warnings
4 use constant _MOUSE_DEBUG => $ENV{MOUSE_DEBUG} ? 1 : 0;
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)
22 && $constraint->has_coercion
23 && $attribute->should_coerce);
25 my $compiled_type_constraint = defined($constraint)
26 ? $constraint->_compiled_type_constraint
30 my $slot = $method_class->_inline_slot($self, $name);;
32 my $accessor = sprintf(qq{package %s;\n#line 1 "%s-accessor for %s (%s)"\n}, $class->name, $type, $name, __FILE__)
35 if ($type eq 'rw' || $type eq 'wo') {
38 'if (scalar(@_) >= 2) {' . "\n";
42 'if(@_ < 2){ Carp::confess("Not enough arguments for the writer of $name") }'.
48 if (defined $constraint) {
52 'my $val = $constraint->coerce('.$value.');';
57 '$compiled_type_constraint->('.$value.') or
58 $attribute->_throw_type_constraint_error('.$value.', $constraint);' . "\n";
61 # if there's nothing left to do for the attribute we can return during
63 $accessor .= 'return ' if !$is_weak && !$trigger && !$should_deref;
65 $accessor .= "$slot = $value;\n";
68 $accessor .= "Scalar::Util::weaken($slot) if ref $slot;\n";
72 $accessor .= '$trigger->('.$self.', '.$value.');' . "\n";
77 elsif($type eq 'ro') {
78 $accessor .= 'Carp::confess("Cannot assign a value to a read-only accessor of $name") if scalar(@_) >= 2;' . "\n";
81 $class->throw_error("Unknown accessor type '$type'");
84 if ($attribute->is_lazy and $type ne 'wo') {
87 if (defined $builder){
88 $value = "$self->\$builder()";
90 elsif (ref($default) eq 'CODE'){
91 $value = "$self->\$default()";
97 $accessor .= "els" if $type eq 'rw';
98 $accessor .= "if(!exists $slot){\n";
100 $accessor .= "$slot = \$constraint->coerce($value)";
102 elsif(defined $constraint){
103 $accessor .= "my \$tmp = $value;\n";
104 $accessor .= "\$compiled_type_constraint->(\$tmp)";
105 $accessor .= " || \$attribute->_throw_type_constraint_error(\$tmp, \$constraint);\n";
106 $accessor .= "$slot = \$tmp;\n";
109 $accessor .= "$slot = $value;\n";
112 $accessor .= "Scalar::Util::weaken($slot) if ref $slot;\n";
118 if ($constraint->is_a_type_of('ArrayRef')) {
119 $accessor .= "return \@{ $slot || [] } if wantarray;\n";
121 elsif($constraint->is_a_type_of('HashRef')){
122 $accessor .= "return \%{ $slot || {} } if wantarray;\n";
125 $class->throw_error("Can not auto de-reference the type constraint " . $constraint->name);
129 $accessor .= "return $slot;\n}\n";
131 warn $accessor if _MOUSE_DEBUG;
135 $code = eval $accessor;
143 sub _generate_accessor{
144 #my($self, $attribute, $metaclass) = @_;
146 return $self->_generate_accessor_any(rw => @_);
149 sub _generate_reader {
150 #my($self, $attribute, $metaclass) = @_;
152 return $self->_generate_accessor_any(ro => @_);
155 sub _generate_writer {
156 #my($self, $attribute, $metaclass) = @_;
158 return $self->_generate_accessor_any(wo => @_);
161 sub _generate_predicate {
162 #my($self, $attribute, $metaclass) = @_;
163 my(undef, $attribute) = @_;
165 my $slot = $attribute->name;
167 return exists $_[0]->{$slot};
171 sub _generate_clearer {
172 #my($self, $attribute, $metaclass) = @_;
173 my(undef, $attribute) = @_;
175 my $slot = $attribute->name;
177 delete $_[0]->{$slot};
186 Mouse::Meta::Method::Accessor - A Mouse method generator for accessors
190 This document describes Mouse version 0.92
194 L<Moose::Meta::Method::Accessor>