| 1 | package Simple::Accessor; | ||||||||||||||||||||||||||||
| 2 | 24 | $Simple::Accessor::VERSION = '1.15'; | |||||||||||||||||||||||||||
| 3 | 24 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 24 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 24 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | # ABSTRACT: a light and simple way to provide accessor in perl | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # VERSION | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||||||||||
| 12 | Simple::Accessor - very simple, light and powerful accessor | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | package Role::Color; | ||||||||||||||||||||||||||||
| 17 | use Simple::Accessor qw{color}; | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | sub _build_color { 'red' } # default color | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | package Car; | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # that s all what you need ! no more line required | ||||||||||||||||||||||||||||
| 24 | use Simple::Accessor qw{brand hp}; | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | with 'Role::Color'; | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | sub _build_hp { 2 } | ||||||||||||||||||||||||||||
| 29 | sub _build_brand { 'unknown' } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | package main; | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | my $c = Car->new( brand => 'zebra' ); | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | is $c->brand, 'zebra'; | ||||||||||||||||||||||||||||
| 36 | is $c->color, 'red'; | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | Simple::Accessor provides a simple object layer without any dependency. | ||||||||||||||||||||||||||||
| 41 | It can be used where other ORM could be considered too heavy. | ||||||||||||||||||||||||||||
| 42 | But it has also the main advantage to only need one single line of code. | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | It can be easily used in scripts... | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =head1 Usage | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | Create a package and just call Simple::Accessor. | ||||||||||||||||||||||||||||
| 49 | The new method will be imported for you, and all accessors will be directly | ||||||||||||||||||||||||||||
| 50 | accessible. | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | package MyClass; | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # that s all what you need ! no more line required | ||||||||||||||||||||||||||||
| 55 | use Simple::Accessor qw{foo bar cherry apple}; | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | You can also split your attribute declarations across multiple C<use> statements. | ||||||||||||||||||||||||||||
| 58 | Attributes from all imports are merged and fully supported by the constructor, | ||||||||||||||||||||||||||||
| 59 | strict constructor mode, and deterministic initialization ordering. | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | package MyClass; | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | use Simple::Accessor qw{foo bar}; | ||||||||||||||||||||||||||||
| 64 | use Simple::Accessor qw{cherry apple}; | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | # all four attributes work in the constructor | ||||||||||||||||||||||||||||
| 67 | my $o = MyClass->new(foo => 1, bar => 2, cherry => 3, apple => 4); | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | Inheritance via C<@ISA> is supported. The constructor recognizes attributes | ||||||||||||||||||||||||||||
| 70 | from parent classes, so you can subclass naturally: | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | package Vehicle; | ||||||||||||||||||||||||||||
| 73 | use Simple::Accessor qw{speed}; | ||||||||||||||||||||||||||||
| 74 | sub _build_speed { 0 } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | package Car; | ||||||||||||||||||||||||||||
| 77 | use parent -norequire, 'Vehicle'; | ||||||||||||||||||||||||||||
| 78 | use Simple::Accessor qw{brand}; | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | package main; | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | my $car = Car->new(brand => 'Tesla', speed => 100); | ||||||||||||||||||||||||||||
| 83 | is $car->speed, 100; # parent attr set via child constructor | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | You can now call 'new' on your class, and create objects using these attributes. | ||||||||||||||||||||||||||||
| 86 | The constructor accepts both a hash and a hashref: | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | package main; | ||||||||||||||||||||||||||||
| 89 | use MyClass; | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | my $o = MyClass->new() | ||||||||||||||||||||||||||||
| 92 | or MyClass->new(bar => 42) | ||||||||||||||||||||||||||||
| 93 | or MyClass->new({ apple => 'fruit', cherry => 'fruit' }); | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | You can get / set any value using the accessor | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | is $o->bar(), 42; | ||||||||||||||||||||||||||||
| 98 | $o->bar(51); | ||||||||||||||||||||||||||||
| 99 | is $o->bar(), 51; | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | You can provide your own init method that will be call by new with default args. | ||||||||||||||||||||||||||||
| 102 | This is optional. | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | package MyClass; | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | sub build { # previously known as initialize | ||||||||||||||||||||||||||||
| 107 | my ($self, %opts) = @_; | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | $self->foo(12345); | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | You can also control the object after or before its creation using | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | sub _before_build { | ||||||||||||||||||||||||||||
| 115 | my ($self, %opts) = @_; | ||||||||||||||||||||||||||||
| 116 | ... | ||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | sub _after_build { | ||||||||||||||||||||||||||||
| 120 | my ($self, %opts) = @_; | ||||||||||||||||||||||||||||
| 121 | ... | ||||||||||||||||||||||||||||
| 122 | bless $self, 'Basket'; | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | You can also provide individual builders / initializers | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | sub _build_bar { # previously known as _initialize_bar | ||||||||||||||||||||||||||||
| 128 | # will be used if no value has been provided for bar | ||||||||||||||||||||||||||||
| 129 | 1031; | ||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | sub _build_cherry { | ||||||||||||||||||||||||||||
| 133 | 'red'; | ||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | You can enable strict constructor mode to catch typos in attribute names: | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | package MyClass; | ||||||||||||||||||||||||||||
| 139 | use Simple::Accessor qw{name age}; | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | sub _strict_constructor { 1 } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | package main; | ||||||||||||||||||||||||||||
| 144 | MyClass->new(nmae => 'oops'); | ||||||||||||||||||||||||||||
| 145 | # dies: "MyClass->new(): unknown attribute(s): nmae" | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | This is opt-in and off by default for backward compatibility. | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | You can even use a very basic but useful hook system. | ||||||||||||||||||||||||||||
| 150 | Any false value returned by before, validate, or after will stop the setting process. | ||||||||||||||||||||||||||||
| 151 | The after hooks include a re-entrancy guard: if an C<_after_*> hook triggers | ||||||||||||||||||||||||||||
| 152 | a setter that would re-enter the same attribute, the nested C<_after_*> call | ||||||||||||||||||||||||||||
| 153 | is skipped to prevent infinite recursion. | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | sub _before_foo { | ||||||||||||||||||||||||||||
| 156 | my ($self, $v) = @_; | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | # do whatever you want with $v | ||||||||||||||||||||||||||||
| 159 | return 1 or 0; | ||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | sub _validate_foo { | ||||||||||||||||||||||||||||
| 163 | my ($self, $v) = @_; | ||||||||||||||||||||||||||||
| 164 | # invalid value ( will not be set ) | ||||||||||||||||||||||||||||
| 165 | return 0 if ( $v == 42); | ||||||||||||||||||||||||||||
| 166 | # valid value | ||||||||||||||||||||||||||||
| 167 | return 1; | ||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | sub _after_cherry { | ||||||||||||||||||||||||||||
| 171 | my ($self) = @_; | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | # use the set value for extra operations | ||||||||||||||||||||||||||||
| 174 | $self->apple($self->cherry()); | ||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =head1 METHODS | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | None. The only public method provided is the classical import. | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =cut | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 24 | my $INFO; | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | # Internal hash key for re-entrancy guard state. Uses a null-byte prefix | ||||||||||||||||||||||||||||
| 186 | # so it can never collide with a user-declared attribute name (valid Perl | ||||||||||||||||||||||||||||
| 187 | # identifiers cannot start with \0). | ||||||||||||||||||||||||||||
| 188 | 24 | my $_GUARD_KEY = "\0_sa_guard"; | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | # Per-class hook/builder cache. Populated lazily on first access so that | ||||||||||||||||||||||||||||
| 191 | # hooks installed after import() still get picked up. Keyed by the actual | ||||||||||||||||||||||||||||
| 192 | # object class (ref $self) so subclass overrides resolve correctly. | ||||||||||||||||||||||||||||
| 193 | # Structure: $_hook_cache->{$class}{"$attr\0$hook"} = $coderef | undef | ||||||||||||||||||||||||||||
| 194 | 24 | my $_hook_cache = {}; | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 174 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 197 | 174 | my ( $class, @attr ) = @_; | |||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 174 | my $from = caller(); | |||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 174 | ▶ | $INFO = {} unless defined $INFO; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | 174 | ▶ | $INFO->{$from} = {} unless defined $INFO->{$from}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | 174 | ▶ | $INFO->{$from}->{'attributes'} ||= []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$INFO->{$from}{"attributes"} ||= []
MC/DC100%1/1
$INFO->{$from}{"attributes"} ||= []
$INFO->{$from}{"attributes"}
Truth table100%2/2
A
A= $INFO->{$from}{"attributes"}
| |||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 174 | _add_with($from); | |||||||||||||||||||||||||||
| 206 | 174 | _add_new($from); | |||||||||||||||||||||||||||
| 207 | 174 | _add_accessors( to => $from, attributes => \@attr ); | |||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | # append after _add_accessors succeeds (it dies on duplicates) | ||||||||||||||||||||||||||||
| 210 | 173 | push @{$INFO->{$from}->{'attributes'}}, @attr; | |||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 173 | return; | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 174 | ▶ | sub _add_with { | ||||||||||||||||||||||||||
|
Subroutine
_add_with: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 216 | 174 | my $class = shift; | |||||||||||||||||||||||||||
| 217 | 174 | ▶ | return unless $class; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 218 | # Check own namespace only (not @ISA) so each SA class gets its own with(). | ||||||||||||||||||||||||||||
| 219 | # can() walks @ISA and would skip children whose parents already have with(), | ||||||||||||||||||||||||||||
| 220 | # but with() is called as a bare function — not a method — so it must exist | ||||||||||||||||||||||||||||
| 221 | # directly in the class's stash. | ||||||||||||||||||||||||||||
| 222 | { | ||||||||||||||||||||||||||||
| 223 | 24 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 224 | 174 | ▶ | return if defined &{"${class}::with"}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | 170 | my $with = $class . '::with'; | |||||||||||||||||||||||||||
| 228 | { | ||||||||||||||||||||||||||||
| 229 | 24 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 230 | 47 | ▶ | *$with = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=13 SCAR=25.7
| |||||||||||||||||||||||||||||
| 231 | 47 | my ( @what ) = @_; | |||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | 47 | ▶ | $INFO->{$class}->{'with'} = [] unless $INFO->{$class}->{'with'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 234 | 47 | push @{$INFO->{$class}->{'with'}}, @what; | |||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 47 | foreach my $module ( @what ) { | |||||||||||||||||||||||||||
| 237 | 48 | ▶ | die "Invalid module name: $module" unless $module =~ /\A[A-Za-z_]\w*(?:::\w+)*\z/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 238 | # skip require if the role is already registered (e.g. inline package) | ||||||||||||||||||||||||||||
| 239 | 46 | ▶ | unless ($INFO->{$module} && $INFO->{$module}->{attributes}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$INFO->{$module} and $INFO->{$module}{"attributes"}
MC/DC50%1/2
$INFO->{$module} and $INFO->{$module}{"attributes"}
$INFO->{$module}
$INFO->{$module}{"attributes"}
Truth table66%2/3
A and B
A= $INFO->{$module}B= $INFO->{$module}{"attributes"}
| |||||||||||||||||||||||||||||
| 240 | 8 | ▶ | eval qq[require $module; 1] or die $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | die "$module is not a Simple::Accessor role" | ||||||||||||||||||||||||||||
| 243 | 44 | ▶ | unless $INFO->{$module} && $INFO->{$module}->{attributes}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$INFO->{$module} and $INFO->{$module}{"attributes"}
MC/DC50%1/2
$INFO->{$module} and $INFO->{$module}{"attributes"}
$INFO->{$module}
$INFO->{$module}{"attributes"}
Truth table66%2/3
A and B
A= $INFO->{$module}B= $INFO->{$module}{"attributes"}
| |||||||||||||||||||||||||||||
| 244 | # Build a role resolution chain for each attribute. | ||||||||||||||||||||||||||||
| 245 | # The chain starts with the immediate role ($module), then | ||||||||||||||||||||||||||||
| 246 | # appends any deeper origins so hooks resolve most-specific | ||||||||||||||||||||||||||||
| 247 | # first. E.g. if MiddleRole overrides _validate_foo from | ||||||||||||||||||||||||||||
| 248 | # OriginRole, the chain is [MiddleRole, OriginRole] — the | ||||||||||||||||||||||||||||
| 249 | # accessor tries MiddleRole first. | ||||||||||||||||||||||||||||
| 250 | 42 | ▶ | my $origins = $INFO->{$module}{attr_origin} || {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$INFO->{$module}{'attr_origin'} || {}
MC/DC100%1/1
$INFO->{$module}{'attr_origin'} || {}
$INFO->{$module}{'attr_origin'}
Truth table100%2/2
A
A= $INFO->{$module}{'attr_origin'}
| |||||||||||||||||||||||||||||
| 251 | 42 | foreach my $att (@{$INFO->{$module}->{attributes}}) { | |||||||||||||||||||||||||||
| 252 | 48 | my @chain = ($module); | |||||||||||||||||||||||||||
| 253 | 48 | ▶ | if (my $parent = $origins->{$att}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 254 | 12 | ▶ | push @chain, ref $parent eq 'ARRAY' ? @$parent : $parent; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||
| 256 | # dedup while preserving order (most-specific first) | ||||||||||||||||||||||||||||
| 257 | 48 | my %seen; | |||||||||||||||||||||||||||
| 258 | 48 | @chain = grep { !$seen{$_}++ } @chain; | |||||||||||||||||||||||||||
| 259 | 48 | _add_accessors( | |||||||||||||||||||||||||||
| 260 | to => $class, | ||||||||||||||||||||||||||||
| 261 | attributes => [$att], | ||||||||||||||||||||||||||||
| 262 | from_roles => \@chain | ||||||||||||||||||||||||||||
| 263 | ); | ||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 41 | return; | |||||||||||||||||||||||||||
| 268 | 170 | }; | |||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | # Collect all attributes for a class, including inherited ones via @ISA. | ||||||||||||||||||||||||||||
| 273 | # Returns an arrayref of unique attribute names in declaration order: | ||||||||||||||||||||||||||||
| 274 | # own attrs first, then parent attrs following Perl's MRO (DFS or C3). | ||||||||||||||||||||||||||||
| 275 | 173 | ▶ | sub _all_attributes { | ||||||||||||||||||||||||||
|
Subroutine
_all_attributes: called CC=6 SCAR=18.1
| |||||||||||||||||||||||||||||
| 276 | 173 | my ($class) = @_; | |||||||||||||||||||||||||||
| 277 | 173 | ▶ | my $own = $INFO->{$class}{attributes} || []; | ||||||||||||||||||||||||||
|
Condition50%1/2
$INFO->{$class}{'attributes'} || []
MC/DC0%0/1
$INFO->{$class}{'attributes'} || []
$INFO->{$class}{'attributes'}
Truth table50%1/2
A
A= $INFO->{$class}{'attributes'}
| |||||||||||||||||||||||||||||
| 278 | 173 | my @all = @{$own}; | |||||||||||||||||||||||||||
| 279 | 173 | my %seen = map { $_ => 1 } @all; | |||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | # use Perl's MRO (respects both default DFS and use mro 'c3') | ||||||||||||||||||||||||||||
| 282 | 173 | require mro; | |||||||||||||||||||||||||||
| 283 | 173 | my $mro = mro::get_linear_isa($class); | |||||||||||||||||||||||||||
| 284 | # skip $class itself (index 0) — already handled above | ||||||||||||||||||||||||||||
| 285 | 173 | for my $i ( 1 .. $#{$mro} ) { | |||||||||||||||||||||||||||
| 286 | 57 | my $parent = $mro->[$i]; | |||||||||||||||||||||||||||
| 287 | 57 | ▶ | my $parent_attrs = $INFO->{$parent}{attributes} || []; | ||||||||||||||||||||||||||
|
Condition50%1/2
$INFO->{$parent}{'attributes'} || []
MC/DC0%0/1
$INFO->{$parent}{'attributes'} || []
$INFO->{$parent}{'attributes'}
Truth table50%1/2
A
A= $INFO->{$parent}{'attributes'}
| |||||||||||||||||||||||||||||
| 288 | 57 | for my $attr ( @{$parent_attrs} ) { | |||||||||||||||||||||||||||
| 289 | 92 | ▶ | push @all, $attr unless $seen{$attr}++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | 173 | return \@all; | |||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | 174 | ▶ | sub _add_new { | ||||||||||||||||||||||||||
|
Subroutine
_add_new: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 297 | 174 | my $class = shift; | |||||||||||||||||||||||||||
| 298 | 174 | ▶ | return unless $class; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 299 | # Same rationale as _add_with: check own stash, not @ISA. | ||||||||||||||||||||||||||||
| 300 | # new() is called as a method (Class->new), so inheritance works, | ||||||||||||||||||||||||||||
| 301 | # but installing per-class ensures correct behavior when a non-SA | ||||||||||||||||||||||||||||
| 302 | # class sits between two SA classes in the MRO. | ||||||||||||||||||||||||||||
| 303 | { | ||||||||||||||||||||||||||||
| 304 | 24 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 305 | 174 | ▶ | return if defined &{"${class}::new"}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | 170 | my $new = $class . '::new'; | |||||||||||||||||||||||||||
| 309 | { | ||||||||||||||||||||||||||||
| 310 | 24 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 311 | 173 | ▶ | *$new = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=13 SCAR=25.7
| |||||||||||||||||||||||||||||
| 312 | 173 | my $class = shift; | |||||||||||||||||||||||||||
| 313 | 173 | ▶ | $class = ref($class) || $class; | ||||||||||||||||||||||||||
|
Condition66%2/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table66%2/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||
| 314 | 173 | ▶ | my %opts = ref $_[0] eq 'HASH' ? %{$_[0]} : @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | 173 | my $self = bless {}, $class; | |||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | 173 | ▶ | if ( $self->can( '_before_build') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 319 | 4 | $self->_before_build( %opts ); | |||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | # includes inherited attributes from parent classes via @ISA | ||||||||||||||||||||||||||||
| 323 | 173 | my $attrs = _all_attributes($class); | |||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | # strict constructor: die on unknown attributes BEFORE setting | ||||||||||||||||||||||||||||
| 326 | # any values, so attribute hooks don't fire with side effects | ||||||||||||||||||||||||||||
| 327 | # that would be discarded when the constructor dies. | ||||||||||||||||||||||||||||
| 328 | 173 | ▶ | if ( $self->can('_strict_constructor') && $self->_strict_constructor() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->can("_strict_constructor") and $self->_strict_constructor
MC/DC100%2/2
$self->can("_strict_constructor") and $self->_strict_constructor
$self->can("_strict_constructor")
$self->_strict_constructor
Truth table100%3/3
A and B
A= $self->can("_strict_constructor")B= $self->_strict_constructor
| |||||||||||||||||||||||||||||
| 329 | 33 | my %known = map { $_ => 1 } @{$attrs}; | |||||||||||||||||||||||||||
| 330 | 33 | my @unknown = sort grep { !$known{$_} } keys %opts; | |||||||||||||||||||||||||||
| 331 | 33 | ▶ | if (@unknown) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 332 | 13 | die "$class\->new(): unknown attribute(s): " | |||||||||||||||||||||||||||
| 333 | . join(', ', @unknown) . "\n"; | ||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | # set values for known attributes (in declaration order) | ||||||||||||||||||||||||||||
| 338 | 160 | foreach my $attr ( @{$attrs} ) { | |||||||||||||||||||||||||||
| 339 | 370 | ▶ | $self->$attr( $opts{$attr} ) if exists $opts{$attr}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | 159 | foreach my $init ( 'build', 'initialize' ) { | |||||||||||||||||||||||||||
| 343 | 311 | ▶ | if ( $self->can( $init ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 344 | 13 | ▶ | return unless $self->$init(%opts); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 345 | 9 | last; # build takes precedence over initialize | |||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | 155 | ▶ | if ( $self->can( '_after_build') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 350 | 3 | $self->_after_build( %opts ); | |||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | 155 | return $self; | |||||||||||||||||||||||||||
| 354 | 170 | }; | |||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | 222 | ▶ | sub _add_accessors { | ||||||||||||||||||||||||||
|
Subroutine
_add_accessors: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||
| 359 | 222 | my (%opts) = @_; | |||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | 222 | ▶ | return unless my $class = $opts{to}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 362 | 222 | my @attributes = @{ $opts{attributes} }; | |||||||||||||||||||||||||||
| 363 | 222 | ▶ | return unless @attributes; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | 213 | ▶ | my $from_roles = $opts{from_roles} || []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$opts{'from_roles'} || []
MC/DC100%1/1
$opts{'from_roles'} || []
$opts{'from_roles'}
Truth table100%2/2
A
A= $opts{'from_roles'}
| |||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | 213 | foreach my $att (@attributes) { | |||||||||||||||||||||||||||
| 368 | 254 | my $accessor = $class . "::" . $att; | |||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | 254 | ▶ | if ( $class->can($att) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 371 | # skip silently when composing roles (duplicates are OK) | ||||||||||||||||||||||||||||
| 372 | 5 | ▶ | next if @$from_roles; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 373 | 1 | die "$class: attribute '$att' is already defined."; | |||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | # track role attributes in the class's attribute list and remember | ||||||||||||||||||||||||||||
| 377 | # which roles defined them (for transitive composition) | ||||||||||||||||||||||||||||
| 378 | 249 | ▶ | if ( @$from_roles ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 379 | 44 | push @{$INFO->{$class}{attributes}}, $att; | |||||||||||||||||||||||||||
| 380 | 44 | $INFO->{$class}{attr_origin}{$att} = $from_roles; | |||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | # allow symbolic refs to typeglob | ||||||||||||||||||||||||||||
| 384 | 24 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 385 | 520 | ▶ | *$accessor = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=25 SCAR=32.2
| |||||||||||||||||||||||||||||
| 386 | 520 | my ( $self, $v ) = @_; | |||||||||||||||||||||||||||
| 387 | 520 | ▶ | if ( @_ > 1 ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 388 | # re-entrancy guard: skip _after_* if we're already setting this attribute | ||||||||||||||||||||||||||||
| 389 | 273 | my $is_reentrant = $self->{$_GUARD_KEY}{$att}; | |||||||||||||||||||||||||||
| 390 | 273 | local $self->{$_GUARD_KEY}{$att} = 1; | |||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | # save old state so _after_* can rollback the set on false return | ||||||||||||||||||||||||||||
| 393 | 273 | my $had_old = exists $self->{$att}; | |||||||||||||||||||||||||||
| 394 | 273 | my $old_val = $self->{$att}; | |||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | 273 | ▶ | my $hc = $_hook_cache->{ref $self} //= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$$_hook_cache{ref $self} //= {}
MC/DC100%1/1
$$_hook_cache{ref $self} //= {}
$$_hook_cache{ref $self}
Truth table100%2/2
A
A= $$_hook_cache{ref $self}
| |||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | 273 | foreach (qw{before validate set after}) { | |||||||||||||||||||||||||||
| 399 | 1038 | ▶ | if ( $_ eq 'set' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 400 | 248 | $self->{$att} = $v; | |||||||||||||||||||||||||||
| 401 | 248 | next; | |||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||
| 403 | 790 | ▶ | if ( $_ eq 'after' && $is_reentrant ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$_ eq "after" and $is_reentrant
MC/DC100%2/2
$_ eq "after" and $is_reentrant
$_ eq "after"
$is_reentrant
Truth table100%3/3
A and B
A= $_ eq "after"B= $is_reentrant
| |||||||||||||||||||||||||||||
| 404 | 4 | next; | |||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | 786 | my $ck = "${att}\0${_}"; | |||||||||||||||||||||||||||
| 408 | 786 | ▶ | unless (exists $hc->{$ck}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 409 | 481 | my $sub = '_' . $_ . '_' . $att; | |||||||||||||||||||||||||||
| 410 | 481 | $hc->{$ck} = $self->can($sub); | |||||||||||||||||||||||||||
| 411 | 481 | ▶ | if (!$hc->{$ck} && @$from_roles) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $hc->{$ck} and @$from_roles
MC/DC100%2/2
not $hc->{$ck} and @$from_roles
not $hc->{$ck}
@$from_roles
Truth table100%3/3
not(A) and B
A= not $hc->{$ck}B= @$from_roles
| |||||||||||||||||||||||||||||
| 412 | 60 | for my $role (@$from_roles) { | |||||||||||||||||||||||||||
| 413 | 80 | ▶ | if (my $code = $role->can($sub)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 414 | 18 | $hc->{$ck} = $code; | |||||||||||||||||||||||||||
| 415 | 18 | last; | |||||||||||||||||||||||||||
| 416 | } | ||||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | 786 | ▶ | if (my $code = $hc->{$ck}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 422 | 157 | ▶ | unless ( $code->($self, $v) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 423 | 30 | ▶ | if ( $_ eq 'after' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 424 | 6 | ▶ | if ($had_old) { $self->{$att} = $old_val } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 425 | 4 | else { delete $self->{$att} } | |||||||||||||||||||||||||||
| 426 | } | ||||||||||||||||||||||||||||
| 427 | 30 | return; | |||||||||||||||||||||||||||
| 428 | } | ||||||||||||||||||||||||||||
| 429 | } | ||||||||||||||||||||||||||||
| 430 | } | ||||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||
| 432 | elsif ( !exists $self->{$att} ) { | ||||||||||||||||||||||||||||
| 433 | # try to initialize the value (try first with build) | ||||||||||||||||||||||||||||
| 434 | # initialize is here for backward compatibility with older versions | ||||||||||||||||||||||||||||
| 435 | 68 | ▶ | my $hc = $_hook_cache->{ref $self} //= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$$_hook_cache{ref $self} //= {}
MC/DC100%1/1
$$_hook_cache{ref $self} //= {}
$$_hook_cache{ref $self}
Truth table100%2/2
A
A= $$_hook_cache{ref $self}
| |||||||||||||||||||||||||||||
| 436 | 68 | foreach my $builder ( qw{build initialize} ) { | |||||||||||||||||||||||||||
| 437 | 87 | my $ck = "${att}\0${builder}"; | |||||||||||||||||||||||||||
| 438 | 87 | ▶ | unless (exists $hc->{$ck}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 439 | 79 | my $sub = '_' . $builder . '_' . $att; | |||||||||||||||||||||||||||
| 440 | 79 | $hc->{$ck} = $self->can($sub); | |||||||||||||||||||||||||||
| 441 | 79 | ▶ | if (!$hc->{$ck} && @$from_roles) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $hc->{$ck} and @$from_roles
MC/DC100%2/2
not $hc->{$ck} and @$from_roles
not $hc->{$ck}
@$from_roles
Truth table100%3/3
not(A) and B
A= not $hc->{$ck}B= @$from_roles
| |||||||||||||||||||||||||||||
| 442 | 23 | for my $role (@$from_roles) { | |||||||||||||||||||||||||||
| 443 | 31 | ▶ | if (my $code = $role->can($sub)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 444 | 21 | $hc->{$ck} = $code; | |||||||||||||||||||||||||||
| 445 | 21 | last; | |||||||||||||||||||||||||||
| 446 | } | ||||||||||||||||||||||||||||
| 447 | } | ||||||||||||||||||||||||||||
| 448 | } | ||||||||||||||||||||||||||||
| 449 | } | ||||||||||||||||||||||||||||
| 450 | 87 | ▶ | if (my $code = $hc->{$ck}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 451 | 50 | return $self->{$att} = $code->($self); | |||||||||||||||||||||||||||
| 452 | } | ||||||||||||||||||||||||||||
| 453 | } | ||||||||||||||||||||||||||||
| 454 | } | ||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | 439 | return $self->{$att}; | |||||||||||||||||||||||||||
| 457 | 249 | }; | |||||||||||||||||||||||||||
| 458 | } | ||||||||||||||||||||||||||||
| 459 | } | ||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | 24 | 1; | |||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | =head1 CONTRIBUTE | ||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | You can contribute to this project on github https://github.com/atoomic/Simple-Accessor | ||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | =head1 AI POLICY | ||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | This project uses AI tools to assist development. Humans review and approve every change before it is merged. See [AI\_POLICY.md](AI_POLICY.md) for details. | ||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | =cut | ||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | __END__ | ||||||||||||||||||||||||||||