1 package Mouse::Meta::Method::Accessor;
6 my (undef, $attribute, $method_name, $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 = $attribute->_inlined_name;
25 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
27 if ($type eq 'accessor' || $type eq 'writer') {
28 if($type eq 'accessor'){
30 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
31 'if (scalar(@_) >= 2) {' . "\n";
35 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
36 'if(@_ < 2){ Carp::confess("Not enough arguments for writer '.$method_name.'") }'.
46 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
47 'my $val = Mouse::Util::TypeConstraints->typecast_constraints("'.$attribute->associated_class->name.'", $attribute->{type_constraint}, '.$value.');';
50 if ($compiled_type_constraint) {
53 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
54 'unless ($compiled_type_constraint->('.$value.')) {
55 $attribute->verify_type_constraint_error($name, '.$value.', $attribute->{type_constraint});
60 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
61 'unless ($constraint->check('.$value.')) {
62 $attribute->verify_type_constraint_error($name, '.$value.', $attribute->{type_constraint});
67 # if there's nothing left to do for the attribute we can return during
69 $accessor .= 'return ' if !$is_weak && !$trigger && !$should_deref;
71 $accessor .= $self.'->{'.$key.'} = '.$value.';' . "\n";
74 $accessor .= 'Scalar::Util::weaken('.$self.'->{'.$key.'}) if ref('.$self.'->{'.$key.'});' . "\n";
78 $accessor .= '$trigger->('.$self.', '.$value.');' . "\n";
83 elsif($type eq 'reader') {
84 $accessor .= 'Carp::confess("Cannot assign a value to a read-only accessor") if scalar(@_) >= 2;' . "\n";
87 $class->throw_error("Unknown accessor type '$type'");
90 if ($attribute->is_lazy) {
91 $accessor .= $self.'->{'.$key.'} = ';
93 $accessor .= $attribute->has_builder
95 : ref($default) eq 'CODE'
96 ? '$default->('.$self.')'
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";
120 my $code = eval $accessor;
121 $attribute->throw_error($@) if $@;
123 $class->add_method($method_name => $code);
129 $class->_install_accessor(@_, 'reader');
135 $class->_install_accessor(@_, 'writer');
140 sub _install_predicate {
141 my (undef, $attribute, $method_name, $class) = @_;
143 my $key = $attribute->_inlined_name;
145 my $predicate = 'sub { exists($_[0]->{'.$key.'}) }';
147 my $code = eval $predicate;
148 $attribute->throw_error($@) if $@;
149 $class->add_method($method_name => $code);
153 sub _install_clearer {
154 my (undef, $attribute, $method_name, $class) = @_;
156 my $key = $attribute->_inlined_name;
158 my $clearer = 'sub { delete($_[0]->{'.$key.'}) }';
160 my $code = eval $clearer;
161 $attribute->throw_error($@) if $@;
162 $class->add_method($method_name => $code);
166 sub _install_handles {
167 my (undef, $attribute, $handles, $class) = @_;
169 my $reader = $attribute->name;
170 my %handles = $attribute->_canonicalize_handles($handles);
174 foreach my $local_method (keys %handles) {
175 my $remote_method = $handles{$local_method};
179 $self->'.$reader.'->'.$remote_method.'(@_)
182 my $code = eval $method;
183 $attribute->throw_error($@) if $@;
185 push @methods, ($local_method => $code);
188 # install after all the method compiled successfully
189 while(my($name, $code) = splice @methods, 0, 2){
190 $class->add_method($name, $code);