| 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.15'; | |||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | sub new { | ||||||||||||||||||||||||||||
| 16 | 89 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | 89 | ▶ | $class = ref $class if ref $class; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 89 | my $proto = $class->BUILDARGS( @_ ); | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 87 | ▶ | Carp::confess('BUILDARGS must return a HASH reference, not '.$proto) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$proto and ref $proto eq "HASH"
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"
| |||||||||||||||||||||||||||||
| 22 | unless $proto && ref $proto eq 'HASH'; | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 87 | my $self = $class->BLESS( $proto ); | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 84 | ▶ | Carp::confess('BLESS must return a blessed reference, not '.$self) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self and UNIVERSAL::isa($self, "UNIVERSAL")
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")
| |||||||||||||||||||||||||||||
| 27 | unless defined $self && UNIVERSAL::isa( $self, 'UNIVERSAL' ); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 84 | ▶ | $self->can('BUILD') && UNIVERSAL::Object::Util::BUILDALL( $self, $proto ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 84 | return $self; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | sub BUILDARGS { | ||||||||||||||||||||||||||||
| 35 | 85 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | 85 | ▶ | if ( scalar @_ == 1 && ref $_[0] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
scalar @_ == 1 and ref $_[0]
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]
| |||||||||||||||||||||||||||||
| 37 | 4 | ▶ | Carp::confess('Invalid BUILDARGS args for '.$class.', expected a HASH reference but got a '.$_[0]) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | unless ref $_[0] eq 'HASH'; | ||||||||||||||||||||||||||||
| 39 | 3 | return +{ %{ $_[0] } }; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | else { | ||||||||||||||||||||||||||||
| 42 | 81 | ▶ | Carp::confess('Invalid BUILDARGS args for '.$class.', expected an even sized list, but got '.(scalar @_).' element(s) instead') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | unless ((scalar @_) % 2) == 0; | ||||||||||||||||||||||||||||
| 44 | 80 | return +{ @_ }; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | sub BLESS { | ||||||||||||||||||||||||||||
| 49 | 85 | ▶ | my $class = $_[0]; | ||||||||||||||||||||||||||
|
Subroutine
BLESS: called CC=7 SCAR=20.7
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | 85 | ▶ | $class = ref $class if ref $class; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 85 | my $proto = $_[1]; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 85 | ▶ | Carp::confess('Invalid BLESS args for '.$class.', You must specify an instance prototype as a HASH ref') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $proto and ref $proto eq "HASH"
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"
| |||||||||||||||||||||||||||||
| 54 | unless defined $proto && ref $proto eq 'HASH'; | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 85 | my $instance = $class->CREATE( $proto ); | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 82 | ▶ | Carp::confess('CREATE must return a reference to bless, not '.$instance) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $instance and ref $instance
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
| |||||||||||||||||||||||||||||
| 59 | unless defined $instance && ref $instance; | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 82 | my $repr = ref $instance; | |||||||||||||||||||||||||||
| 62 | 82 | 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 | 82 | ▶ | if ( $repr eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 67 | my %slots = $self->SLOTS; | |||||||||||||||||||||||||||
| 71 | 67 | Hash::Util::lock_keys( %$self, keys %slots ); | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 82 | 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 28 | 1; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | __END__ | ||||||||||||||||||||||||||||