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";
101 $accessor .= "\$compiled_type_constraint->(\$tmp)";
102 $accessor .= " || \$attribute->_throw_type_constraint_error(\$tmp, \$constraint);\n";
103 $accessor .= "$slot = \$tmp;\n";
106 $accessor .= "$slot = $value;\n";
109 $accessor .= "Scalar::Util::weaken($slot) if ref $slot;\n";
115 if ($constraint->is_a_type_of('ArrayRef')) {
116 $accessor .= "return \@{ $slot || [] } if wantarray;\n";
118 elsif($constraint->is_a_type_of('HashRef')){
119 $accessor .= "return \%{ $slot || {} } if wantarray;\n";
122 $class->throw_error("Can not auto de-reference the type constraint " . $constraint->name);
126 $accessor .= "return $slot;\n}\n";
128 warn $accessor if _MOUSE_DEBUG;
132 $code = eval $accessor;
140 sub _generate_accessor{
141 #my($self, $attribute, $metaclass) = @_;
143 return $self->_generate_accessor_any(rw => @_);
146 sub _generate_reader {
147 #my($self, $attribute, $metaclass) = @_;
149 return $self->_generate_accessor_any(ro => @_);
152 sub _generate_writer {
153 #my($self, $attribute, $metaclass) = @_;
155 return $self->_generate_accessor_any(wo => @_);
158 sub _generate_predicate {
159 #my($self, $attribute, $metaclass) = @_;
160 my(undef, $attribute) = @_;
162 my $slot = $attribute->name;
164 return exists $_[0]->{$slot};
168 sub _generate_clearer {
169 #my($self, $attribute, $metaclass) = @_;
170 my(undef, $attribute) = @_;
172 my $slot = $attribute->name;
174 delete $_[0]->{$slot};
183 Mouse::Meta::Method::Accessor - A Mouse method generator for accessors
187 This document describes Mouse version 0.73
191 L<Moose::Meta::Method::Accessor>