1 package Mouse::Meta::Method::Accessor;
2 use Mouse::Util qw(:meta); # enables strict and warnings
5 my(undef, $self_var, $attr_name) = @_;
6 return sprintf '%s->{q{%s}}', $self_var, $attr_name;
9 sub _generate_accessor_any{
10 my($method_class, $type, $attribute, $class) = @_;
12 my $name = $attribute->name;
13 my $default = $attribute->default;
14 my $constraint = $attribute->type_constraint;
15 my $builder = $attribute->builder;
16 my $trigger = $attribute->trigger;
17 my $is_weak = $attribute->is_weak_ref;
18 my $should_deref = $attribute->should_auto_deref;
19 my $should_coerce = (defined($constraint) && $constraint->has_coercion && $attribute->should_coerce);
21 my $compiled_type_constraint = defined($constraint) ? $constraint->_compiled_type_constraint : undef;
24 my $slot = $method_class->_inline_slot($self, $name);;
26 my $accessor = sprintf(qq{package %s;\n#line 1 "%s-accessor for %s (%s)"\n}, $class->name, $type, $name, __FILE__)
29 if ($type eq 'rw' || $type eq 'wo') {
32 'if (scalar(@_) >= 2) {' . "\n";
36 'if(@_ < 2){ Carp::confess("Not enough arguments for the writer of '.$name.'") }'.
42 if (defined $constraint) {
46 'my $val = $constraint->coerce('.$value.');';
51 '$compiled_type_constraint->('.$value.') or
52 $attribute->_throw_type_constraint_error('.$value.', $constraint);' . "\n";
55 # if there's nothing left to do for the attribute we can return during
57 $accessor .= 'return ' if !$is_weak && !$trigger && !$should_deref;
59 $accessor .= "$slot = $value;\n";
62 $accessor .= "Scalar::Util::weaken($slot) if ref $slot;\n";
66 $accessor .= '$trigger->('.$self.', '.$value.');' . "\n";
71 elsif($type eq 'ro') {
72 $accessor .= 'Carp::confess("Cannot assign a value to a read-only accessor") if scalar(@_) >= 2;' . "\n";
75 $class->throw_error("Unknown accessor type '$type'");
78 if ($attribute->is_lazy) {
81 if (defined $builder){
82 $value = "$self->\$builder()";
84 elsif (ref($default) eq 'CODE'){
85 $value = "$self->\$default()";
91 $accessor .= "if(!exists $slot){\n";
93 $accessor .= "$slot = \$constraint->coerce($value)";
95 elsif(defined $constraint){
96 $accessor .= "my \$tmp = $value;\n";
98 $accessor .= "\$compiled_type_constraint->(\$tmp)";
99 $accessor .= " || \$attribute->_throw_type_constraint_error(\$tmp, \$constraint);\n";
100 $accessor .= "$slot = \$tmp;\n";
103 $accessor .= "$slot = $value;\n";
106 $accessor .= "Scalar::Util::weaken($slot) if ref $slot;\n";
112 if ($constraint->is_a_type_of('ArrayRef')) {
113 $accessor .= "return \@{ $slot || [] } if wantarray;\n";
115 elsif($constraint->is_a_type_of('HashRef')){
116 $accessor .= "return \%{ $slot || {} } if wantarray;\n";
119 $class->throw_error("Can not auto de-reference the type constraint " . $constraint->name);
123 $accessor .= "return $slot;\n}\n";
125 #print $accessor, "\n";
129 $code = eval $accessor;
137 sub _generate_accessor{
139 return $class->_generate_accessor_any(rw => @_);
142 sub _generate_reader {
144 return $class->_generate_accessor_any(ro => @_);
147 sub _generate_writer {
149 return $class->_generate_accessor_any(wo => @_);
152 sub _generate_predicate {
153 my (undef, $attribute, $class) = @_;
155 my $slot = $attribute->name;
157 return exists $_[0]->{$slot};
161 sub _generate_clearer {
162 my (undef, $attribute, $class) = @_;
164 my $slot = $attribute->name;
166 delete $_[0]->{$slot};
175 Mouse::Meta::Method::Accessor - A Mouse method generator for accessors
179 This document describes Mouse version 0.45
183 L<Moose::Meta::Method::Accessor>