1 package Mouse::Meta::Method::Accessor;
2 use Mouse::Util; # enables strict and warnings
3 use Scalar::Util qw(blessed);
6 my(undef, $self_var, $attr_name) = @_;
7 return sprintf '%s->{q{%s}}', $self_var, $attr_name;
10 sub _generate_accessor{
11 my ($method_class, $attribute, $class, $type) = @_;
13 my $name = $attribute->name;
14 my $default = $attribute->default;
15 my $constraint = $attribute->type_constraint;
16 my $builder = $attribute->builder;
17 my $trigger = $attribute->trigger;
18 my $is_weak = $attribute->is_weak_ref;
19 my $should_deref = $attribute->should_auto_deref;
20 my $should_coerce = (defined($constraint) && $constraint->has_coercion && $attribute->should_coerce);
22 my $compiled_type_constraint = defined($constraint) ? $constraint->_compiled_type_constraint : undef;
25 my $slot = $method_class->_inline_slot($self, $name);;
29 my $accessor = sprintf(qq{#line 1 "%s for %s (%s)"\n}, $type, $name, __FILE__)
32 if ($type eq 'accessor' || $type eq 'writer') {
33 if($type eq 'accessor'){
35 'if (scalar(@_) >= 2) {' . "\n";
39 'if(@_ < 2){ Carp::confess("Not enough arguments for the writer of '.$name.'") }'.
45 if (defined $constraint) {
49 'my $val = $constraint->coerce('.$value.');';
54 '$compiled_type_constraint->('.$value.') or
55 $attribute->verify_type_constraint_error($name, '.$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 .= "$slot = $value;\n";
65 $accessor .= "Scalar::Util::weaken($slot) if ref $slot;\n";
69 $accessor .= '$trigger->('.$self.', '.$value.');' . "\n";
74 elsif($type eq 'reader') {
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 = "$self->\$builder()";
87 elsif (ref($default) eq 'CODE'){
88 $value = "$self->\$default()";
94 $accessor .= "if(!exists $slot){\n";
96 $accessor .= "$slot = \$constraint->coerce($value)";
98 elsif(defined $constraint){
99 $accessor .= "my \$tmp = $value;\n";
100 #XXX: The following 'defined and' check is for backward compatibility
101 $accessor .= "defined(\$tmp) and ";
103 $accessor .= "\$compiled_type_constraint->(\$tmp)";
104 $accessor .= " || \$attribute->verify_type_constraint_error(\$name, \$tmp, \$constraint);\n";
105 $accessor .= "$slot = \$tmp;\n";
108 $accessor .= "$slot = $value;\n";
114 if ($constraint->is_a_type_of('ArrayRef')) {
115 $accessor .= "return \@{ $slot || [] } if wantarray;\n";
117 elsif($constraint->is_a_type_of('HashRef')){
118 $accessor .= "return \%{ $slot || {} } if wantarray;\n";
121 $class->throw_error("Can not auto de-reference the type constraint " . $constraint->name);
125 $accessor .= "return $slot;\n}\n";
127 #print "# class ", $class->name, "\n", $accessor, "\n";
131 $code = eval $accessor;
139 sub _generate_reader{
141 return $class->_generate_accessor(@_, 'reader');
144 sub _generate_writer{
146 return $class->_generate_accessor(@_, 'writer');
150 sub _generate_predicate {
151 my (undef, $attribute, $class) = @_;
153 my $slot = $attribute->name;
155 return exists $_[0]->{$slot};
159 sub _generate_clearer {
160 my (undef, $attribute, $class) = @_;
162 my $slot = $attribute->name;
165 delete $_[0]->{$slot};
169 sub _generate_delegation{
170 my (undef, $attribute, $class, $reader, $handle_name, $method_to_call) = @_;
173 my $instance = shift;
174 my $proxy = $instance->$reader();
176 my $error = !defined($proxy) ? ' is not defined'
177 : ref($proxy) && !blessed($proxy) ? qq{ is not an object (got '$proxy')}
180 $instance->meta->throw_error(
181 "Cannot delegate $handle_name to $method_to_call because "
187 $proxy->$method_to_call(@_);
197 Mouse::Meta::Method::Accessor - A Mouse method generator for accessors
201 This document describes Mouse version 0.40
205 L<Moose::Meta::Method::Accessor>