| 1 | package Mouse::Meta::Method::Accessor; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use Mouse::Util qw(:meta); # enables strict and warnings | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use constant _MOUSE_DEBUG => $ENV{MOUSE_DEBUG} ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 0 | ▶ | sub _inline_slot{ | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_inline_slot: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 0 | my(undef, $self_var, $attr_name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 8 | 0 | return sprintf '%s->{q{%s}}', $self_var, $attr_name; | |||||||||||||||||||||||||||||||||||||||||
| 9 | } | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 0 | ▶ | sub _generate_accessor_any{ | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_generate_accessor_any: not called CC=28 SCAR=67.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 0 | my($method_class, $type, $attribute, $class) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 0 | my $name = $attribute->name; | |||||||||||||||||||||||||||||||||||||||||
| 15 | 0 | my $default = $attribute->default; | |||||||||||||||||||||||||||||||||||||||||
| 16 | 0 | my $constraint = $attribute->type_constraint; | |||||||||||||||||||||||||||||||||||||||||
| 17 | 0 | my $builder = $attribute->builder; | |||||||||||||||||||||||||||||||||||||||||
| 18 | 0 | my $trigger = $attribute->trigger; | |||||||||||||||||||||||||||||||||||||||||
| 19 | 0 | my $is_weak = $attribute->is_weak_ref; | |||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | my $should_deref = $attribute->should_auto_deref; | |||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | ▶ | my $should_coerce = (defined($constraint) | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/6
defined $constraint && $constraint->has_coercion && $attribute->should_coerce
defined $constraint and $constraint->has_coercion
MC/DC0%0/3
defined $constraint && $constraint->has_coercion && $attribute->should_coerce
defined $constraint
$constraint->has_coercion
$attribute->should_coerce
Truth table0%0/4
A and B && C
A= defined $constraintB= $constraint->has_coercionC= $attribute->should_coerce
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | && $constraint->has_coercion | ||||||||||||||||||||||||||||||||||||||||||
| 23 | && $attribute->should_coerce); | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | ▶ | my $compiled_type_constraint = defined($constraint) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | ? $constraint->_compiled_type_constraint | ||||||||||||||||||||||||||||||||||||||||||
| 27 | : undef; | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | my $self = '$_[0]'; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | my $slot = $method_class->_inline_slot($self, $name);; | |||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | my $accessor = sprintf(qq{package %s;\n#line 1 "%s-accessor for %s (%s)"\n}, $class->name, $type, $name, __FILE__) | |||||||||||||||||||||||||||||||||||||||||
| 33 | . "sub {\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | if ($type eq 'rw' || $type eq 'wo') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$type eq "rw" or $type eq "wo"
MC/DC0%0/2
$type eq "rw" or $type eq "wo"
$type eq "rw"
$type eq "wo"
Truth table0%0/3
A or B
A= $type eq "rw"B= $type eq "wo"
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | if($type eq 'rw'){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | $accessor .= | |||||||||||||||||||||||||||||||||||||||||
| 38 | 'if (scalar(@_) >= 2) {' . "\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||
| 40 | else{ # writer | ||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | $accessor .= | |||||||||||||||||||||||||||||||||||||||||
| 42 | 'if(@_ < 2){ Carp::confess("Not enough arguments for the writer of $name") }'. | ||||||||||||||||||||||||||||||||||||||||||
| 43 | '{' . "\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | my $value = '$_[1]'; | |||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | if (defined $constraint) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | if ($should_coerce) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | $accessor .= | |||||||||||||||||||||||||||||||||||||||||
| 51 | "\n". | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 'my $val = $constraint->coerce('.$value.');'; | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | $value = '$val'; | |||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | $accessor .= | ||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | "\n". | |||||||||||||||||||||||||||||||||||||||||
| 57 | '$compiled_type_constraint->('.$value.') or | ||||||||||||||||||||||||||||||||||||||||||
| 58 | $attribute->_throw_type_constraint_error('.$value.', $constraint);' . "\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | # if there's nothing left to do for the attribute we can return during | ||||||||||||||||||||||||||||||||||||||||||
| 62 | # this setter | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | $accessor .= 'return ' if !$is_weak && !$trigger && !$should_deref; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$is_weak or $trigger or $should_deref
$is_weak or $trigger
MC/DC0%0/3
$is_weak or $trigger or $should_deref
$is_weak
$trigger
$should_deref
Truth table0%0/4
A or B or C
A= $is_weakB= $triggerC= $should_deref
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | $accessor .= "my \@old_value = exists $slot ? $slot : ();\n" if $trigger; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | $accessor .= "$slot = $value;\n"; | |||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | if ($is_weak) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | $accessor .= "Scalar::Util::weaken($slot) if ref $slot;\n"; | |||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | if ($trigger) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | $accessor .= '$trigger->('.$self.', '.$value.', @old_value);' . "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | $accessor .= "}\n"; | |||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | elsif($type eq 'ro') { | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | $accessor .= 'Carp::confess("Cannot assign a value to a read-only accessor of $name") if scalar(@_) >= 2;' . "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | else{ | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | $class->throw_error("Unknown accessor type '$type'"); | |||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | if ($attribute->is_lazy and $type ne 'wo') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$attribute->is_lazy and $type ne "wo"
MC/DC0%0/2
$attribute->is_lazy and $type ne "wo"
$attribute->is_lazy
$type ne "wo"
Truth table0%0/3
A and B
A= $attribute->is_lazyB= $type ne "wo"
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | my $value; | |||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | if (defined $builder){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | $value = "$self->\$builder()"; | |||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | elsif (ref($default) eq 'CODE'){ | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | $value = "$self->\$default()"; | |||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | else{ | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | $value = '$default'; | |||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | $accessor .= "els" if $type eq 'rw'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | $accessor .= "if(!exists $slot){\n"; | |||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | if($should_coerce){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | $accessor .= "$slot = \$constraint->coerce($value)"; | |||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | elsif(defined $constraint){ | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | $accessor .= "my \$tmp = $value;\n"; | |||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | $accessor .= "\$compiled_type_constraint->(\$tmp)"; | |||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | $accessor .= " || \$attribute->_throw_type_constraint_error(\$tmp, \$constraint);\n"; | |||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | $accessor .= "$slot = \$tmp;\n"; | |||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | else{ | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | $accessor .= "$slot = $value;\n"; | |||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | if ($is_weak) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | $accessor .= "Scalar::Util::weaken($slot) if ref $slot;\n"; | |||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | $accessor .= "}\n"; | |||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | if ($should_deref) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | if ($constraint->is_a_type_of('ArrayRef')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | $accessor .= "return \@{ $slot || [] } if wantarray;\n"; | |||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 122 | elsif($constraint->is_a_type_of('HashRef')){ | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | $accessor .= "return \%{ $slot || {} } if wantarray;\n"; | |||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||
| 125 | else{ | ||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | $class->throw_error("Can not auto de-reference the type constraint " . $constraint->name); | |||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | $accessor .= "return $slot;\n}\n"; | |||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | warn $accessor if _MOUSE_DEBUG; | |||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | my $code; | |||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | my $e = do{ | |||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | local $@; | |||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | $code = eval $accessor; | |||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | $@; | |||||||||||||||||||||||||||||||||||||||||
| 138 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | ▶ | die $e if $e; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | return $code; | |||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | ▶ | sub _generate_accessor{ | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_generate_accessor: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | #my($self, $attribute, $metaclass) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | return $self->_generate_accessor_any(rw => @_); | |||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | ▶ | sub _generate_reader { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_generate_reader: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | #my($self, $attribute, $metaclass) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | return $self->_generate_accessor_any(ro => @_); | |||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | ▶ | sub _generate_writer { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_generate_writer: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | #my($self, $attribute, $metaclass) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | return $self->_generate_accessor_any(wo => @_); | |||||||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | ▶ | sub _generate_predicate { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_generate_predicate: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | #my($self, $attribute, $metaclass) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | my(undef, $attribute) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | my $slot = $attribute->name; | |||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | ▶ | return sub{ | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | return exists $_[0]->{$slot}; | |||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | 0 | ▶ | sub _generate_clearer { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_generate_clearer: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | #my($self, $attribute, $metaclass) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | my(undef, $attribute) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | my $slot = $attribute->name; | |||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | ▶ | return sub{ | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | delete $_[0]->{$slot}; | |||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 183 | __END__ | ||||||||||||||||||||||||||||||||||||||||||