| 1 | package HO::class; | ||||||||||||||||||||||||||||
| 2 | # **************** | ||||||||||||||||||||||||||||
| 3 | 13 | ▶ | use strict; use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 13 | our $VERSION='0.079'; | |||||||||||||||||||||||||||
| 5 | # ******************* | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | ; require HO::accessor | ||||||||||||||||||||||||||||
| 8 | 13 | ; require Carp | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 13 | ; our (%mixin_classes,%class_args,%class_methods) | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | ; sub import | ||||||||||||||||||||||||||||
| 13 | 31 | ▶ | { my ($package,@args)=@_ | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=10 SCAR=23.1
| |||||||||||||||||||||||||||||
| 14 | 31 | ; our (%mixin_classes,%class_args,%class_methods) | |||||||||||||||||||||||||||
| 15 | 31 | ; my $makeconstr = 1 | |||||||||||||||||||||||||||
| 16 | ; # uncoverable branch false | ||||||||||||||||||||||||||||
| 17 | # uncoverable condition right | ||||||||||||||||||||||||||||
| 18 | # uncoverable condition false | ||||||||||||||||||||||||||||
| 19 | 31 | ▶ | my $class = $HO::accessor::class || | ||||||||||||||||||||||||||
|
Condition66%2/3
$HO::accessor::class || caller
MC/DC0%0/2
$HO::accessor::class || caller
-$HO::accessor::class
-caller
Truth table66%2/3
A || B
A= $HO::accessor::classB= caller
| |||||||||||||||||||||||||||||
| 20 | CORE::caller # uncoverable statement | ||||||||||||||||||||||||||||
| 21 | ; my @acc # all internal accessors | ||||||||||||||||||||||||||||
| 22 | 31 | ; my @methods # method changeable on a per object base | |||||||||||||||||||||||||||
| 23 | ; my @lvalue # lvalue accessor | ||||||||||||||||||||||||||||
| 24 | 31 | ; my @r_ # common accessors | |||||||||||||||||||||||||||
| 25 | 31 | ; my $makeinit # key for init method or subref used as init | |||||||||||||||||||||||||||
| 26 | ; my @alias | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 31 | ; $class_methods{$class} = {} | |||||||||||||||||||||||||||
| 29 | 31 | ; $class_args{$class} = [ @args ] | |||||||||||||||||||||||||||
| 30 | 31 | ▶ | ; if($mixin_classes{$class}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 1 | { push @args, @{$mixin_classes{$class}} | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 31 | ▶ | ; while(@args) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 43 | { my $action = lc(shift @args) | |||||||||||||||||||||||||||
| 36 | 43 | ; my ($name,$type,$code) | |||||||||||||||||||||||||||
| 37 | ;({ '_method' => sub | ||||||||||||||||||||||||||||
| 38 | 4 | ▶ | { ($name,$code) = splice(@args,0,2) | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 39 | 4 | ▶ | ; push @acc, "__$name",sub { $code } if defined $code | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 4 | ; push @acc, "_$name",'$' | |||||||||||||||||||||||||||
| 41 | 4 | ; push @methods, $name, $code | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | , '_index' => sub | ||||||||||||||||||||||||||||
| 44 | 3 | ▶ | { ($name,$type) = splice(@args,0,2) | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | 3 | ; push @acc, $name, $type | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | , '_lvalue' => sub | ||||||||||||||||||||||||||||
| 48 | 1 | ▶ | { ($name,$type) = splice(@args,0,2) | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 49 | 1 | ; push @acc, "_$name", $type | |||||||||||||||||||||||||||
| 50 | 1 | ; push @lvalue, $name | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | , '_rw' => sub | ||||||||||||||||||||||||||||
| 53 | 12 | ▶ | { ($name,$type) = splice(@args,0,2) | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 54 | 12 | ; push @acc, "_$name", $type | |||||||||||||||||||||||||||
| 55 | 12 | ▶ | ; if(defined($args[0]) && lc($args[0]) eq 'abstract') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $args[0] and lc $args[0] eq "abstract"
MC/DC100%2/2
defined $args[0] and lc $args[0] eq "abstract"
defined $args[0]
lc $args[0] eq "abstract"
Truth table100%3/3
A and B
A= defined $args[0]B= lc $args[0] eq "abstract"
| |||||||||||||||||||||||||||||
| 56 | { shift @args | ||||||||||||||||||||||||||||
| 57 | 1 | } | |||||||||||||||||||||||||||
| 58 | else | ||||||||||||||||||||||||||||
| 59 | 11 | ▶ | { $type = _type_of($type) if ref($type) eq 'CODE' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 11 | ; my $coderef = $HO::accessor::rw_accessor{$type} | |||||||||||||||||||||||||||
| 61 | 11 | ▶ | ; Carp::croak("Unknown property type '$type', in setup for class $class.") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | unless defined $coderef | ||||||||||||||||||||||||||||
| 63 | 11 | ; push @r_, $name => [ $coderef, $name, $class ] | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | , '_ro' => sub | ||||||||||||||||||||||||||||
| 67 | 17 | ▶ | { ($name,$type) = splice(@args,0,2) | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 68 | 17 | ; push @acc, "_$name", $type | |||||||||||||||||||||||||||
| 69 | # abstract is similar to _index, but there is TIMTOWTDI | ||||||||||||||||||||||||||||
| 70 | 17 | ▶ | ; if(defined($args[0]) && lc($args[0]) eq 'abstract') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $args[0] and lc $args[0] eq "abstract"
MC/DC100%2/2
defined $args[0] and lc $args[0] eq "abstract"
defined $args[0]
lc $args[0] eq "abstract"
Truth table100%3/3
A and B
A= defined $args[0]B= lc $args[0] eq "abstract"
| |||||||||||||||||||||||||||||
| 71 | { shift @args | ||||||||||||||||||||||||||||
| 72 | 1 | } | |||||||||||||||||||||||||||
| 73 | else | ||||||||||||||||||||||||||||
| 74 | 16 | ▶ | { $type = _type_of($type) if ref($type) eq 'CODE' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 16 | ; my $coderef = $HO::accessor::ro_accessor{$type} | |||||||||||||||||||||||||||
| 76 | 16 | ▶ | ; Carp::croak("Unknown property type '$type', in setup for class $class.") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | unless defined $coderef | ||||||||||||||||||||||||||||
| 78 | 15 | ; push @r_, $name => [ $coderef, $name, $class ] | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | , 'init' => sub | ||||||||||||||||||||||||||||
| 82 | 3 | ▶ | { $makeinit = shift @args | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | # no actions => options | ||||||||||||||||||||||||||||
| 85 | , 'noconstructor' => sub | ||||||||||||||||||||||||||||
| 86 | 2 | ▶ | { $makeconstr = 0 | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | , 'alias' => sub | ||||||||||||||||||||||||||||
| 89 | 1 | ▶ | { push @alias, splice(@args,0,2) | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 0 | ▶ | }->{$action}||sub { die "Unknown action '$action' for $package." | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 92 | 43 | ▶ | })->() | ||||||||||||||||||||||||||
|
Condition33%1/3
{'_method', sub {
($name, $code) = splice(@args, 0, 2);
push @acc, "__$name", sub {
$code;
}
if defined $code;
push @acc, "_$name", '$';
push @methods, $name, $code;
}
, '_index', sub {
($name, $type) = splice(@args, 0, 2);
push @acc, $name, $type;
}
, '_lvalue', sub {
($name, $type) = splice(@args, 0, 2);
push @acc, "_$name", $type;
push @lvalue, $name;
}
, '_rw', sub {
($name, $type) = splice(@args, 0, 2);
push @acc, "_$name", $type;
if (defined $args[0] and lc $args[0] eq 'abstract') {
shift @args;
}
else {
$type = &_type_of($type) if ref $type eq 'CODE';
my $coderef = $HO::accessor::rw_accessor{$type};
Carp::croak("Unknown property type '${type}', in setup for class $class.") unless defined $coderef;
push @r_, $name, [$coderef, $name, $class];
};
}
, '_ro', sub {
($name, $type) = splice(@args, 0, 2);
push @acc, "_$name", $type;
if (defined $args[0] and lc $args[0] eq 'abstract') {
shift @args;
}
else {
$type = &_type_of($type) if ref $type eq 'CODE';
my $coderef = $HO::accessor::ro_accessor{$type};
Carp::croak("Unknown property type '${type}', in setup for class $class.") unless defined $coderef;
push @r_, $name, [$coderef, $name, $class];
};
}
, 'init', sub {
$makeinit = shift @args;
}
, 'noconstructor', sub {
$makeconstr = 0;
}
, 'alias', sub {
push @alias, splice(@args, 0, 2);
}
}->{$action} || sub {
die "Unknown action '${action}' for $package.";
}
MC/DC0%0/2
{'_method', sub {
($name, $code) = splice(@args, 0, 2);
push @acc, "__$name", sub {
$code;
}
if defined $code;
push @acc, "_$name", '$';
push @methods, $name, $code;
}
, '_index', sub {
($name, $type) = splice(@args, 0, 2);
push @acc, $name, $type;
}
, '_lvalue', sub {
($name, $type) = splice(@args, 0, 2);
push @acc, "_$name", $type;
push @lvalue, $name;
}
, '_rw', sub {
($name, $type) = splice(@args, 0, 2);
push @acc, "_$name", $type;
if (defined $args[0] and lc $args[0] eq 'abstract') {
shift @args;
}
else {
$type = &_type_of($type) if ref $type eq 'CODE';
my $coderef = $HO::accessor::rw_accessor{$type};
Carp::croak("Unknown property type '${type}', in setup for class $class.") unless defined $coderef;
push @r_, $name, [$coderef, $name, $class];
};
}
, '_ro', sub {
($name, $type) = splice(@args, 0, 2);
push @acc, "_$name", $type;
if (defined $args[0] and lc $args[0] eq 'abstract') {
shift @args;
}
else {
$type = &_type_of($type) if ref $type eq 'CODE';
my $coderef = $HO::accessor::ro_accessor{$type};
Carp::croak("Unknown property type '${type}', in setup for class $class.") unless defined $coderef;
push @r_, $name, [$coderef, $name, $class];
};
}
, 'init', sub {
$makeinit = shift @args;
}
, 'noconstructor', sub {
$makeconstr = 0;
}
, 'alias', sub {
push @alias, splice(@args, 0, 2);
}
}->{$action} || sub {
die "Unknown action '${action}' for $package.";
}
{'_method', sub {
($name, $code) = splice(@args, 0, 2);
push @acc, "__$name", sub {
$code;
}
if defined $code;
push @acc, "_$name", '$';
push @methods, $name, $code;
}
, '_index', sub {
($name, $type) = splice(@args, 0, 2);
push @acc, $name, $type;
}
, '_lvalue', sub {
($name, $type) = splice(@args, 0, 2);
push @acc, "_$name", $type;
push @lvalue, $name;
}
, '_rw', sub {
($name, $type) = splice(@args, 0, 2);
push @acc, "_$name", $type;
if (defined $args[0] and lc $args[0] eq 'abstract') {
shift @args;
}
else {
$type = &_type_of($type) if ref $type eq 'CODE';
my $coderef = $HO::accessor::rw_accessor{$type};
Carp::croak("Unknown property type '${type}', in setup for class $class.") unless defined $coderef;
push @r_, $name, [$coderef, $name, $class];
};
}
, '_ro', sub {
($name, $type) = splice(@args, 0, 2);
push @acc, "_$name", $type;
if (defined $args[0] and lc $args[0] eq 'abstract') {
shift @args;
}
else {
$type = &_type_of($type) if ref $type eq 'CODE';
my $coderef = $HO::accessor::ro_accessor{$type};
Carp::croak("Unknown property type '${type}', in setup for class $class.") unless defined $coderef;
push @r_, $name, [$coderef, $name, $class];
};
}
, 'init', sub {
$makeinit = shift @args;
}
, 'noconstructor', sub {
$makeconstr = 0;
}
, 'alias', sub {
push @alias, splice(@args, 0, 2);
}
}->{$action}
sub {
die "Unknown action '${action}' for $package.";
}
Truth table33%1/3
A || B
A= {'_method', sub {
($name, $code) = splice(@args, 0, 2);
push @acc, "__$name", sub {
$code;
}
if defined $code;
push @acc, "_$name", '$';
push @methods, $name, $code;
}
, '_index', sub {
($name, $type) = splice(@args, 0, 2);
push @acc, $name, $type;
}
, '_lvalue', sub {
($name, $type) = splice(@args, 0, 2);
push @acc, "_$name", $type;
push @lvalue, $name;
}
, '_rw', sub {
($name, $type) = splice(@args, 0, 2);
push @acc, "_$name", $type;
if (defined $args[0] and lc $args[0] eq 'abstract') {
shift @args;
}
else {
$type = &_type_of($type) if ref $type eq 'CODE';
my $coderef = $HO::accessor::rw_accessor{$type};
Carp::croak("Unknown property type '${type}', in setup for class $class.") unless defined $coderef;
push @r_, $name, [$coderef, $name, $class];
};
}
, '_ro', sub {
($name, $type) = splice(@args, 0, 2);
push @acc, "_$name", $type;
if (defined $args[0] and lc $args[0] eq 'abstract') {
shift @args;
}
else {
$type = &_type_of($type) if ref $type eq 'CODE';
my $coderef = $HO::accessor::ro_accessor{$type};
Carp::croak("Unknown property type '${type}', in setup for class $class.") unless defined $coderef;
push @r_, $name, [$coderef, $name, $class];
};
}
, 'init', sub {
$makeinit = shift @args;
}
, 'noconstructor', sub {
$makeconstr = 0;
}
, 'alias', sub {
push @alias, splice(@args, 0, 2);
}
}->{$action}B= sub {
die "Unknown action '${action}' for $package.";
}
| |||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | 30 | ; { local $HO::accessor::class = $class | |||||||||||||||||||||||||||
| 95 | 30 | ; import HO::accessor:: (\@acc,\@methods,$makeinit,$makeconstr) | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 13 | ; { no strict 'refs' | |||||||||||||||||||||||||||
| 99 | 13 | ▶ | ; while(@methods) | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 4 | { my ($name,$code) = splice(@methods,0,2) | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 4 | ; my ($nidx,$ncdx) = ("_$name","__$name") | |||||||||||||||||||||||||||
| 103 | 4 | ; my $idx = HO::accessor::_value_of($class, $nidx) | |||||||||||||||||||||||||||
| 104 | 4 | ; my $cdx = HO::accessor::_value_of($class, $ncdx) | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 4 | ▶ | ; if(defined $cdx) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 4 | { *{join('::',$class,$name)} = sub | |||||||||||||||||||||||||||
| 108 | 6 | ▶ | { my $self = shift | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 109 | 6 | ▶ | ; return $self->[$idx] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | ? $self->[$idx]->($self,@_) | ||||||||||||||||||||||||||||
| 111 | : $self->[$cdx]->($self,@_) | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | 4 | } | |||||||||||||||||||||||||||
| 114 | else | ||||||||||||||||||||||||||||
| 115 | 0 | { *{join('::',$class,$name)} = sub | |||||||||||||||||||||||||||
| 116 | 0 | ▶ | { my $self = shift | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 117 | 0 | ; return $self->[$idx]->($self,@_) | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | 0 | } | |||||||||||||||||||||||||||
| 120 | 4 | ; $class_methods{$class}{$name} = "_method" | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 29 | ▶ | ; while(@lvalue) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 1 | { my $name = shift(@lvalue) | |||||||||||||||||||||||||||
| 125 | 1 | ; my $acc = "_$name" | |||||||||||||||||||||||||||
| 126 | # lvalue methods are inherited not copied, so they needs to have dynamic index | ||||||||||||||||||||||||||||
| 127 | 1 | ; *{join('::',$class,$name)} = sub : lvalue | |||||||||||||||||||||||||||
| 128 | 1 | ▶ | { my $self = shift(); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 129 | 1 | ; $self->[$self->$acc] | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | 1 | ; $class_methods{$class}{$name} = "_lvalue" | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | 29 | ▶ | ; while(my ($name,$subdata) = splice(@r_,0,2)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 25 | { my ($coderef,$name,$class) = @$subdata | |||||||||||||||||||||||||||
| 135 | 25 | ; *{join('::',$class,$name)} = $coderef->($name,$class) | |||||||||||||||||||||||||||
| 136 | 24 | ; $class_methods{$class}{$name} = "_data" | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | 28 | ▶ | ; while(my ($new,$subname) = splice(@alias,0,2)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 1 | { my $code = HO::accessor::_methods_code($class, $subname) | |||||||||||||||||||||||||||
| 140 | 1 | ; *{join('::',$class,$new)} = $code | |||||||||||||||||||||||||||
| 141 | 1 | ; $class_methods{$class}{$new} = "_alias" | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | ; sub _type_of ($) | ||||||||||||||||||||||||||||
| 147 | 8 | ▶ | { my $coderef = shift | ||||||||||||||||||||||||||
|
Subroutine
_type_of: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 148 | 8 | ; my $val = $coderef->() | |||||||||||||||||||||||||||
| 149 | 8 | ▶ | ; return ref($val) eq 'HASH' ? '%' : | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 150 | ref($val) eq 'ARRAY' ? '@' : '$' | ||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 13 | ; 1 | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | __END__ | ||||||||||||||||||||||||||||