blib/lib/Object/Properties.pm

filter: Statementstmt 100% 89 / 89 Branchbran 94.4% 17 / 18 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 21 / 21 Podpod n/a totaltotal 99.2% 132 / 133 CC 17 SCAR 15.8
CC
17
Cov
99%
import
11.0
__ANON__
6.0
__ANON__
2.0
CRAP 4.8
↑ Summary
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
BranchTrueFalseTotal
unless exists $$hash{$field[$i]} 25 113 138
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
BranchTrueFalseTotal
if $hash != $self 0 42 42
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
BranchTrueFalseTotal
if (@prop and "CODE" eq ref $_) 23 28 51
Condition100%3/3
@prop and "CODE" eq ref $_
!ll&&!rl&&r
91923
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 $_
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless /\A([+]?)([^\W\d]\w*)\z/ 1 27 28
63 27 ▶ $ro{ $2 } = 1 unless $1;
Branch: 1 branch
BranchTrueFalseTotal
unless $1 23 4 27
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
BranchTrueFalseTotal
$ro{$prop} ? : 23 4 27
$setter ? : 3 1 4
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
BranchTrueFalseTotal
if (my(@sprop) = grep({exists $setter{$_};} @prop)) 7 1 8
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
BranchTrueFalseTotal
unless @$ISA 2 6 8
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__
↑ Summary