| 1 | 2 | ▶ | use 5.006; # for us | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use 5.008008; # for Sentinel | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | package Object::Properties; | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | our $VERSION = '1.003'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Sentinel (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 7 | ▶ | sub _make_init { | ||||||||||||||||||||||||||
|
Subroutine
_make_init: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 7 | my @field = @{ $_[0] }; | |||||||||||||||||||||||||||
| 14 | 7 | my @setter = @{ $_[1] }; | |||||||||||||||||||||||||||
| 15 | 42 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 16 | 42 | my $self = shift; | |||||||||||||||||||||||||||
| 17 | 42 | my ( $hash ) = @_; | |||||||||||||||||||||||||||
| 18 | 42 | my ( @v, @s ); | |||||||||||||||||||||||||||
| 19 | 42 | for my $i ( 0 .. $#field ) { | |||||||||||||||||||||||||||
| 20 | 138 | ▶ | next unless exists $hash->{ $field[ $i ] }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 113 | push @s, $setter[ $i ]; | |||||||||||||||||||||||||||
| 22 | 113 | push @v, delete $hash->{ $field[ $i ] }; | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | 42 | ▶ | delete @$self{ @field } if $hash != $self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 42 | for my $i ( 0 .. $#v ) { $self->$_( $v[$i] ) for $s[$i] } | |||||||||||||||||||||||||||
| 26 | 7 | }; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 27 | ▶ | sub _make_getter { | ||||||||||||||||||||||||||
|
Subroutine
_make_getter: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 27 | my ( $prop ) = @_; | |||||||||||||||||||||||||||
| 31 | 27 | ▶ | return sub { $_[0]{ $prop } }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 1 | ▶ | sub _make_getter_setter { | ||||||||||||||||||||||||||
|
Subroutine
_make_getter_setter: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 1 | my ( $prop ) = @_; | |||||||||||||||||||||||||||
| 36 | 1 | ▶ | return sub : lvalue { $_[0]{ $prop } }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 23 | ▶ | sub _make_setter { | ||||||||||||||||||||||||||
|
Subroutine
_make_setter: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 23 | my ( $prop, $munger ) = @_; | |||||||||||||||||||||||||||
| 41 | 116 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 42 | 116 | local $Carp::Internal{ (__PACKAGE__) } = 1; | |||||||||||||||||||||||||||
| 43 | 116 | $_[0]{ $prop } = $_, return for &$munger; | |||||||||||||||||||||||||||
| 44 | 23 | }; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 3 | ▶ | sub _make_accessor { | ||||||||||||||||||||||||||
|
Subroutine
_make_accessor: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 48 | 3 | my ( $getter, $setter ) = @_; | |||||||||||||||||||||||||||
| 49 | 3 | ▶ | return sub : lvalue { Sentinel::sentinel get => $getter, set => $setter, obj => $_[0] }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 9 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||||
| 53 | 9 | my $class = shift; | |||||||||||||||||||||||||||
| 54 | 9 | my $pkg = caller; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 9 | my ( @prop, %ro, %setter ); | |||||||||||||||||||||||||||
| 57 | 9 | for ( @_ ) { | |||||||||||||||||||||||||||
| 58 | 51 | ▶ | if ( @prop and 'CODE' eq ref ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@prop and "CODE" eq ref $_
MC/DC100%2/2
@prop and "CODE" eq ref $_
@prop
"CODE" eq ref $_
Truth table100%3/3
A and B
A= @propB= "CODE" eq ref $_
| |||||||||||||||||||||||||||||
| 59 | 23 | $setter{ $prop[-1] } = _make_setter $prop[-1], $_; | |||||||||||||||||||||||||||
| 60 | 23 | next; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 28 | ▶ | die "Invalid accessor name '$_'" unless /\A([+]?)([^\W\d]\w*)\z/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 27 | ▶ | $ro{ $2 } = 1 unless $1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 27 | push @prop, $2; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 8 | for my $prop ( @prop ) { | |||||||||||||||||||||||||||
| 68 | 27 | my $getter = _make_getter $prop; | |||||||||||||||||||||||||||
| 69 | 27 | my $setter = $setter{ $prop }; | |||||||||||||||||||||||||||
| 70 | my $accessor | ||||||||||||||||||||||||||||
| 71 | 27 | ▶ | = $ro{ $prop } ? $getter | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 72 | : $setter ? _make_accessor $getter, $setter | ||||||||||||||||||||||||||||
| 73 | : _make_getter_setter $prop; | ||||||||||||||||||||||||||||
| 74 | 2 | ▶ | { no strict 'refs'; *{ $pkg.'::'.$prop } = $accessor } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 8 | ▶ | if ( my @sprop = grep { exists $setter{ $_ } } @prop ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 7 | my $init = _make_init \@sprop, [ @setter{ @sprop } ]; | |||||||||||||||||||||||||||
| 79 | 2 | ▶ | { no strict 'refs'; *{ $pkg.'::PROPINIT' } = $init } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 2 | ▶ | my $ISA = do { no strict 'refs'; \@{ $pkg.'::ISA' } }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 83 | 8 | ▶ | @$ISA = __PACKAGE__ . '::Base' unless @$ISA; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 8 | return 1; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | package Object::Properties::Base; | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 2 | our $VERSION = '1.003'; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 2 | ▶ | use NEXT (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 42 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 95 | 42 | my $class = shift; | |||||||||||||||||||||||||||
| 96 | 42 | my $self = bless { @_ }, $class; | |||||||||||||||||||||||||||
| 97 | 42 | local $Carp::Internal{ (__PACKAGE__) } = 1; | |||||||||||||||||||||||||||
| 98 | 42 | $self->EVERY::LAST::PROPINIT( $self ); | |||||||||||||||||||||||||||
| 99 | 40 | return $self; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 2 | 1; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | __END__ | ||||||||||||||||||||||||||||