1 package Mouse::Meta::Method::Accessor;
4 use Scalar::Util qw(blessed);
6 sub _generate_accessor{
7 my (undef, $attribute, $class, $type) = @_;
9 my $name = $attribute->name;
10 my $default = $attribute->default;
11 my $constraint = $attribute->type_constraint;
12 my $builder = $attribute->builder;
13 my $trigger = $attribute->trigger;
14 my $is_weak = $attribute->is_weak_ref;
15 my $should_deref = $attribute->should_auto_deref;
16 my $should_coerce = $attribute->should_coerce;
18 my $compiled_type_constraint = $constraint ? $constraint->_compiled_type_constraint : undef;
21 my $key = sprintf q{"%s"}, quotemeta $name;
26 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
29 if ($type eq 'accessor' || $type eq 'writer') {
30 if($type eq 'accessor'){
32 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
33 'if (scalar(@_) >= 2) {' . "\n";
37 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
38 'if(@_ < 2){ Carp::confess("Not enough arguments for the writer of '.$name.'") }'.
44 if (defined $constraint) {
45 if(!$compiled_type_constraint){
46 Carp::confess("[BUG] Missing compiled type constraint for $constraint");
51 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
52 'my $val = $constraint->coerce('.$value.');';
57 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
58 'unless ($compiled_type_constraint->('.$value.')) {
59 $attribute->verify_type_constraint_error($name, '.$value.', $attribute->{type_constraint});
63 # if there's nothing left to do for the attribute we can return during
65 $accessor .= 'return ' if !$is_weak && !$trigger && !$should_deref;
67 $accessor .= $self.'->{'.$key.'} = '.$value.';' . "\n";
70 $accessor .= 'Scalar::Util::weaken('.$self.'->{'.$key.'}) if ref('.$self.'->{'.$key.'});' . "\n";
74 $accessor .= '$trigger->('.$self.', '.$value.');' . "\n";
79 elsif($type eq 'reader') {
80 $accessor .= 'Carp::confess("Cannot assign a value to a read-only accessor") if scalar(@_) >= 2;' . "\n";
83 $class->throw_error("Unknown accessor type '$type'");
86 if ($attribute->is_lazy) {
87 $accessor .= $self.'->{'.$key.'} = ';
89 if($should_coerce && defined($constraint)){
90 $accessor .= '$attribute->_coerce_and_verify(';
92 $accessor .= $attribute->has_builder ? $self.'->$builder'
93 : ref($default) eq 'CODE' ? '$default->('.$self.')'
96 if($should_coerce && defined $constraint){
99 $accessor .= ' if !exists '.$self.'->{'.$key.'};' . "\n";
103 if ($constraint->is_a_type_of('ArrayRef')) {
104 $accessor .= 'if (wantarray) {
105 return @{ '.$self.'->{'.$key.'} || [] };
108 elsif($constraint->is_a_type_of('HashRef')){
109 $accessor .= 'if (wantarray) {
110 return %{ '.$self.'->{'.$key.'} || {} };
114 $class->throw_error("Can not auto de-reference the type constraint " . $constraint->name);
118 $accessor .= 'return '.$self.'->{'.$key."};\n}";
120 #print $accessor, "\n";
124 $code = eval $accessor;
132 sub _generate_reader{
134 return $class->_generate_accessor(@_, 'reader');
137 sub _generate_writer{
139 return $class->_generate_accessor(@_, 'writer');
143 sub _generate_predicate {
144 my (undef, $attribute, $class) = @_;
146 my $slot = $attribute->name;
148 return exists $_[0]->{$slot};
152 sub _generate_clearer {
153 my (undef, $attribute, $class) = @_;
155 my $slot = $attribute->name;
158 delete $_[0]->{$slot};
162 sub _generate_delegation{
163 my (undef, $attribute, $class, $reader, $handle_name, $method_to_call) = @_;
166 my $instance = shift;
167 my $proxy = $instance->$reader();
169 my $error = !defined($proxy) ? ' is not defined'
170 : ref($proxy) && !blessed($proxy) ? qq{ is not an object (got '$proxy')}
173 $instance->meta->throw_error(
174 "Cannot delegate $handle_name to $method_to_call because "
180 $proxy->$method_to_call(@_);