Object.pm

filter: Statementstmt 100% 79 / 79 Branchbran 80.5% 29 / 36 Conditioncond 46.6% 7 / 15 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 100% 17 / 17 Podpod 100% 6 / 6 totaltotal 85.8% 140 / 163 SCAR 33.4
CC
27
Cov
88%
new
8.0
BLESS
8.0
BUILDARGS
5.0
CRAP 28.1
↑ Summary Object/Immutable.pm »
1 package UNIVERSAL::Object;
2 # ABSTRACT: A useful base class
3 28 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 28 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 28 ▶use Hash::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 28 our $VERSION = '0.16';
11 28 our $AUTHORITY = 'cpan:STEVAN';
12  
13 28 ▶BEGIN { $] >= 5.010 ? require mro : require MRO::Compat }
Subroutine BEGIN: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$] >= 5.01 ? : 28 0 28
14  
15 sub new {
16 90 ▶ my $class = shift;
Subroutine new: called CC=7 SCAR=20.8
Pod documented
17 90 ▶ $class = ref $class if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 1 89 90
18  
19 90 my $proto = $class->BUILDARGS( @_ );
20  
21 88 ▶ Carp::confess('BUILDARGS must return a HASH reference, not '.$proto)
Branch: 1 branch
BranchTrueFalseTotal
unless $proto and ref $proto eq "HASH" 0 88 88
Condition33%1/3
$proto and ref $proto eq "HASH"
!ll&&!rl&&r
0088
MC/DC0%0/2
$proto and ref $proto eq "HASH"
$proto ref $proto eq "HASH"
Truth table33%1/3
A and B
A= $protoB= ref $proto eq "HASH"
ABresult
0X0
100
111
22 unless $proto && ref $proto eq 'HASH';
23  
24 88 my $self = $class->BLESS( $proto );
25  
26 85 ▶ Carp::confess('BLESS must return a blessed reference, not '.$self)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self and UNIVERSAL::isa($self, "UNIVERSAL") 0 85 85
Condition33%1/3
defined $self and UNIVERSAL::isa($self, "UNIVERSAL")
!ll&&!rl&&r
0085
MC/DC0%0/2
defined $self and UNIVERSAL::isa($self, "UNIVERSAL")
defined $self UNIVERSAL::isa($self, "UNIVERSAL")
Truth table33%1/3
A and B
A= defined $selfB= UNIVERSAL::isa($self, "UNIVERSAL")
ABresult
0X0
100
111
27 unless defined $self && UNIVERSAL::isa( $self, 'UNIVERSAL' );
28  
29 85 ▶ $self->can('BUILD') && UNIVERSAL::Object::Util::BUILDALL( $self, $proto );
Branch: 1 branch
BranchTrueFalseTotal
$self->can("BUILD") and UNIVERSAL::Object::Util::BUILDALL($self, $proto) 4 81 85
30  
31 85 return $self;
32 }
33  
34 sub BUILDARGS {
35 86 ▶ my $class = shift;
Subroutine BUILDARGS: called CC=5 SCAR=16.1
Pod documented
36 86 ▶ if ( scalar @_ == 1 && ref $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1 and ref $_[0]) { } 4 82 86
Condition100%3/3
scalar @_ == 1 and ref $_[0]
!ll&&!rl&&r
8114
MC/DC100%2/2
scalar @_ == 1 and ref $_[0]
scalar @_ == 1 ref $_[0]
Truth table100%3/3
A and B
A= scalar @_ == 1B= ref $_[0]
ABresult
0X0
100
111
37 4 ▶ Carp::confess('Invalid BUILDARGS args for '.$class.', expected a HASH reference but got a '.$_[0])
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "HASH" 1 3 4
38 unless ref $_[0] eq 'HASH';
39 3 return +{ %{ $_[0] } };
40 }
41 else {
42 82 ▶ Carp::confess('Invalid BUILDARGS args for '.$class.', expected an even sized list, but got '.(scalar @_).' element(s) instead')
Branch: 1 branch
BranchTrueFalseTotal
unless scalar(@_) % 2 == 0 1 81 82
43 unless ((scalar @_) % 2) == 0;
44 81 return +{ @_ };
45 }
46 }
47  
48 sub BLESS {
49 86 ▶ my $class = $_[0];
Subroutine BLESS: called CC=7 SCAR=20.7
Pod documented
50 86 ▶ $class = ref $class if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 86 86
51 86 my $proto = $_[1];
52  
53 86 ▶ Carp::confess('Invalid BLESS args for '.$class.', You must specify an instance prototype as a HASH ref')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $proto and ref $proto eq "HASH" 0 86 86
Condition33%1/3
defined $proto and ref $proto eq "HASH"
!ll&&!rl&&r
0086
MC/DC0%0/2
defined $proto and ref $proto eq "HASH"
defined $proto ref $proto eq "HASH"
Truth table33%1/3
A and B
A= defined $protoB= ref $proto eq "HASH"
ABresult
0X0
100
111
54 unless defined $proto && ref $proto eq 'HASH';
55  
56 86 my $instance = $class->CREATE( $proto );
57  
58 83 ▶ Carp::confess('CREATE must return a reference to bless, not '.$instance)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $instance and ref $instance 0 83 83
Condition33%1/3
defined $instance and ref $instance
!ll&&!rl&&r
0083
MC/DC0%0/2
defined $instance and ref $instance
defined $instance ref $instance
Truth table33%1/3
A and B
A= defined $instanceB= ref $instance
ABresult
0X0
100
111
59 unless defined $instance && ref $instance;
60  
61 83 my $repr = ref $instance;
62 83 my $self = bless $instance => $class;
63  
64 # So,... for HASH based instances we'll
65 # lock the set of keys so as to prevent
66 # typos and other such silliness, if
67 # you use other $repr types, you are
68 # on your own, ... sorry ¯\_(ツ)_/¯
69 83 ▶ if ( $repr eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($repr eq "HASH") 67 16 83
70 67 my %slots = $self->SLOTS;
71 67 Hash::Util::lock_keys( %$self, keys %slots );
72 }
73  
74 83 return $self;
75 }
76  
77 sub CREATE {
78 76 ▶ my $class = $_[0];
Subroutine CREATE: called CC=4 SCAR=13.9
Pod documented
79 76 ▶ $class = ref $class if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 76 76
80 76 my $proto = $_[1];
81  
82 76 my $self = $class->REPR( $proto );
83 76 my %slots = $class->SLOTS;
84  
85 # NOTE:
86 # We could check the return values of SLOTS
87 # and REPR, but they might change and so it
88 # is not something we would always know.
89 # - SL
90  
91 $self->{ $_ } = exists $proto->{ $_ }
92 ? $proto->{ $_ }
93 : $slots{ $_ }->( $self, $proto )
94 76 ▶ foreach sort keys %slots;
Branch: 1 branch
BranchTrueFalseTotal
exists $proto->{$_} ? : 62 104 166
95  
96 73 return $self;
97 }
98  
99 64 ▶sub REPR () { +{} }
Subroutine REPR: called CC=1 SCAR=0.0
Pod documented
100  
101 sub SLOTS {
102 140 ▶ my $class = $_[0];
Subroutine SLOTS: called CC=2 SCAR=6.9
Pod documented
103 140 ▶ $class = ref $class if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 65 75 140
104 28 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
105 28 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
106 140 return %{$class . '::HAS'};
107 }
108  
109 sub DESTROY {
110 65 ▶ my $self = $_[0];
Subroutine DESTROY: called CC=2 SCAR=6.9
111 65 ▶ $self->can('DEMOLISH') && UNIVERSAL::Object::Util::DEMOLISHALL( $self );
Branch: 1 branch
BranchTrueFalseTotal
$self->can("DEMOLISH") and UNIVERSAL::Object::Util::DEMOLISHALL($self) 3 62 65
112 65 return;
113 }
114  
115 ## Utils
116  
117 sub UNIVERSAL::Object::Util::BUILDALL {
118 4 ▶ my $self = $_[0];
Subroutine BUILDALL: called CC=3 SCAR=11.0
119 4 my $proto = $_[1];
120 4 foreach my $super ( reverse @{ mro::get_linear_isa( ref $self ) } ) {
121 11 my $fully_qualified_name = $super . '::BUILD';
122 $self->$fully_qualified_name( $proto )
123 11 ▶ if defined &{ $fully_qualified_name };
Branch: 1 branch
BranchTrueFalseTotal
if defined &{$fully_qualified_name;} 7 4 11
124 }
125 }
126  
127 sub UNIVERSAL::Object::Util::DEMOLISHALL {
128 3 ▶ my $self = $_[0];
Subroutine DEMOLISHALL: called CC=3 SCAR=11.0
129 3 foreach my $super ( @{ mro::get_linear_isa( ref $self ) } ) {
130 9 my $fully_qualified_name = $super . '::DEMOLISH';
131 $self->$fully_qualified_name()
132 9 ▶ if defined &{ $fully_qualified_name };
Branch: 1 branch
BranchTrueFalseTotal
if defined &{$fully_qualified_name;} 6 3 9
133 }
134 }
135  
136 28 1;
137  
138 __END__
↑ Summary Object/Immutable.pm »