1 package Mouse::Meta::Method::Accessor;
2 use Mouse::Util qw(:meta); # enables strict and warnings
3 use warnings FATAL => 'recursion';
5 use Mouse::Meta::Method::Constructor; # for slot access
7 sub _generate_accessor_any{
8 my($method_class, $type, $attribute, $class) = @_;
10 my $c = 'Mouse::Meta::Method::Constructor';
12 my $key = $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;
23 my $instance = '$_[0]';
24 my $slot = $c->_inline_get_slot($instance, $key);;
26 my $accessor = sprintf(<<'END_SUB_START', $class->name, __LINE__, $type, $key, __FILE__);
28 #line %d "%s-accessor for %s (%s)
32 if ($type eq 'rw' || $type eq 'wo') {
35 'if (scalar(@_) >= 2) {' . "\n";
39 'if(@_ < 2){ Carp::confess("Not enough arguments for the writer of '.$key.'") }'.
45 if (defined $constraint) {
49 'my $val = $constraint->coerce('.$value.');';
54 '$compiled_type_constraint->('.$value.') or
55 $attribute->_throw_type_constraint_error('.$value.', $constraint);' . "\n";
58 # if there's nothing left to do for the attribute we can return during
60 $accessor .= 'return ' if !$is_weak && !$trigger && !$should_deref;
62 $accessor .= $c->_inline_set_slot($instance, $key, $value) . ";\n";
65 $accessor .= $c->_inline_weaken_slot($instance, $key) ." if ref $slot;\n";
69 $accessor .= '$trigger->('.$instance.', '.$value.');' . "\n";
74 elsif($type eq 'ro') {
75 $accessor .= 'Carp::confess("Cannot assign a value to a read-only accessor") if scalar(@_) >= 2;' . "\n";
78 $class->throw_error("Unknown accessor type '$type'");
81 if ($attribute->is_lazy) {
84 if (defined $builder){
85 $value = "$instance->\$builder()";
87 elsif (ref($default) eq 'CODE'){
88 $value = "$instance->\$default()";
94 $accessor .= sprintf "if(!%s){\n", $c->_inline_has_slot($instance, $key);
96 $value = "\$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";
106 $accessor .= $c->_inline_set_slot($instance, $key, $value) .";\n";
109 $accessor .= $c->_inline_weaken_slot($instance, $key) . " 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 #print $accessor, "\n";
132 $code = eval $accessor;
140 sub _generate_accessor{
142 return $class->_generate_accessor_any(rw => @_);
145 sub _generate_reader {
147 return $class->_generate_accessor_any(ro => @_);
150 sub _generate_writer {
152 return $class->_generate_accessor_any(wo => @_);
155 sub _generate_predicate {
156 my (undef, $attribute, $class) = @_;
158 my $slot = $attribute->name;
160 return exists $_[0]->{$slot};
164 sub _generate_clearer {
165 my (undef, $attribute, $class) = @_;
167 my $slot = $attribute->name;
169 delete $_[0]->{$slot};
178 Mouse::Meta::Method::Accessor - A Mouse method generator for accessors
182 This document describes Mouse version 0.50_05
186 L<Moose::Meta::Method::Accessor>