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 = $attribute->should_coerce;
17 my $compiled_type_constraint = $constraint ? $constraint->_compiled_type_constraint : undef;
20 my $key = sprintf q{"%s"}, quotemeta $name;
25 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
28 if ($type eq 'accessor' || $type eq 'writer') {
29 if($type eq 'accessor'){
31 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
32 'if (scalar(@_) >= 2) {' . "\n";
36 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
37 'if(@_ < 2){ Carp::confess("Not enough arguments for the writer of '.$name.'") }'.
43 if (defined $constraint) {
44 if(!$compiled_type_constraint){
45 Carp::confess("[BUG] Missing compiled type constraint for $constraint");
50 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
51 'my $val = $constraint->coerce('.$value.');';
56 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
57 'unless ($compiled_type_constraint->('.$value.')) {
58 $attribute->verify_type_constraint_error($name, '.$value.', $attribute->{type_constraint});
62 # if there's nothing left to do for the attribute we can return during
64 $accessor .= 'return ' if !$is_weak && !$trigger && !$should_deref;
66 $accessor .= $self.'->{'.$key.'} = '.$value.';' . "\n";
69 $accessor .= 'Scalar::Util::weaken('.$self.'->{'.$key.'}) if ref('.$self.'->{'.$key.'});' . "\n";
73 $accessor .= '$trigger->('.$self.', '.$value.');' . "\n";
78 elsif($type eq 'reader') {
79 $accessor .= 'Carp::confess("Cannot assign a value to a read-only accessor") if scalar(@_) >= 2;' . "\n";
82 $class->throw_error("Unknown accessor type '$type'");
85 if ($attribute->is_lazy) {
86 $accessor .= $self.'->{'.$key.'} = ';
88 if($should_coerce && defined($constraint)){
89 $accessor .= '$attribute->_coerce_and_verify(';
91 $accessor .= $attribute->has_builder ? $self.'->$builder'
92 : ref($default) eq 'CODE' ? '$default->('.$self.')'
95 if($should_coerce && defined $constraint){
98 $accessor .= ' if !exists '.$self.'->{'.$key.'};' . "\n";
102 if ($constraint->is_a_type_of('ArrayRef')) {
103 $accessor .= 'if (wantarray) {
104 return @{ '.$self.'->{'.$key.'} || [] };
107 elsif($constraint->is_a_type_of('HashRef')){
108 $accessor .= 'if (wantarray) {
109 return %{ '.$self.'->{'.$key.'} || {} };
113 $class->throw_error("Can not auto de-reference the type constraint " . $constraint->name);
117 $accessor .= 'return '.$self.'->{'.$key."};\n}";
119 #print $accessor, "\n";
123 $code = eval $accessor;
131 sub _generate_reader{
133 return $class->_generate_accessor(@_, 'reader');
136 sub _generate_writer{
138 return $class->_generate_accessor(@_, 'writer');
142 sub _generate_predicate {
143 my (undef, $attribute, $class) = @_;
145 my $slot = $attribute->name;
147 return exists $_[0]->{$slot};
151 sub _generate_clearer {
152 my (undef, $attribute, $class) = @_;
154 my $slot = $attribute->name;
157 delete $_[0]->{$slot};
161 sub _generate_delegation{
162 my (undef, $attribute, $class, $reader, $handle_name, $method_to_call) = @_;
165 my $instance = shift;
166 my $proxy = $instance->$reader();
168 my $error = !defined($proxy) ? ' is not defined'
169 : ref($proxy) && !blessed($proxy) ? qq{ is not an object (got '$proxy')}
172 $instance->meta->throw_error(
173 "Cannot delegate $handle_name to $method_to_call because "
179 $proxy->$method_to_call(@_);