1 package Mouse::Meta::Method::Accessor;
2 use Mouse::Util; # enables strict and warnings
3 use Scalar::Util qw(blessed);
5 sub _generate_accessor{
6 my (undef, $attribute, $class, $type) = @_;
8 my $name = $attribute->name;
9 my $default = $attribute->default;
10 my $constraint = $attribute->type_constraint;
11 my $builder = $attribute->builder;
12 my $trigger = $attribute->trigger;
13 my $is_weak = $attribute->is_weak_ref;
14 my $should_deref = $attribute->should_auto_deref;
15 my $should_coerce = (defined($constraint) && $constraint->has_coercion && $attribute->should_coerce);
17 my $compiled_type_constraint = defined($constraint) ? $constraint->_compiled_type_constraint : undef;
21 my $slot = "$self\->{$key}";
25 my $accessor = sprintf(qq{#line 1 "%s for %s (%s)"\n}, $type, $name, __FILE__)
28 if ($type eq 'accessor' || $type eq 'writer') {
29 if($type eq 'accessor'){
31 'if (scalar(@_) >= 2) {' . "\n";
35 'if(@_ < 2){ Carp::confess("Not enough arguments for the writer of '.$name.'") }'.
41 if (defined $constraint) {
45 'my $val = $constraint->coerce('.$value.');';
50 '$compiled_type_constraint->('.$value.') or
51 $attribute->verify_type_constraint_error($name, '.$value.', $constraint);' . "\n";
54 # if there's nothing left to do for the attribute we can return during
56 $accessor .= 'return ' if !$is_weak && !$trigger && !$should_deref;
58 $accessor .= "$slot = $value;\n";
61 $accessor .= "Scalar::Util::weaken($slot) if ref $slot;\n";
65 $accessor .= '$trigger->('.$self.', '.$value.');' . "\n";
70 elsif($type eq 'reader') {
71 $accessor .= 'Carp::confess("Cannot assign a value to a read-only accessor") if scalar(@_) >= 2;' . "\n";
74 $class->throw_error("Unknown accessor type '$type'");
77 if ($attribute->is_lazy) {
80 if (defined $builder){
81 $value = "$self->\$builder()";
83 elsif (ref($default) eq 'CODE'){
84 $value = "$self->\$default()";
90 $accessor .= "if(!exists $slot){\n";
92 $accessor .= "$slot = \$constraint->coerce($value)";
94 elsif(defined $constraint){
95 $accessor .= "my \$tmp = $value;\n";
96 #XXX: The following 'defined and' check is for backward compatibility
97 $accessor .= "defined(\$tmp) and ";
99 $accessor .= "\$compiled_type_constraint->(\$tmp)";
100 $accessor .= " || \$attribute->verify_type_constraint_error(\$name, \$tmp, \$constraint);\n";
101 $accessor .= "$slot = \$tmp;\n";
104 $accessor .= "$slot = $value;\n";
110 if ($constraint->is_a_type_of('ArrayRef')) {
111 $accessor .= "return \@{ $slot || [] } if wantarray;\n";
113 elsif($constraint->is_a_type_of('HashRef')){
114 $accessor .= "return \%{ $slot || {} } if wantarray;\n";
117 $class->throw_error("Can not auto de-reference the type constraint " . $constraint->name);
121 $accessor .= "return $slot;\n}\n";
123 #print "# class ", $class->name, "\n", $accessor, "\n";
127 $code = eval $accessor;
135 sub _generate_reader{
137 return $class->_generate_accessor(@_, 'reader');
140 sub _generate_writer{
142 return $class->_generate_accessor(@_, 'writer');
146 sub _generate_predicate {
147 my (undef, $attribute, $class) = @_;
149 my $slot = $attribute->name;
151 return exists $_[0]->{$slot};
155 sub _generate_clearer {
156 my (undef, $attribute, $class) = @_;
158 my $slot = $attribute->name;
161 delete $_[0]->{$slot};
165 sub _generate_delegation{
166 my (undef, $attribute, $class, $reader, $handle_name, $method_to_call) = @_;
169 my $instance = shift;
170 my $proxy = $instance->$reader();
172 my $error = !defined($proxy) ? ' is not defined'
173 : ref($proxy) && !blessed($proxy) ? qq{ is not an object (got '$proxy')}
176 $instance->meta->throw_error(
177 "Cannot delegate $handle_name to $method_to_call because "
183 $proxy->$method_to_call(@_);