Member/HASH.pm

filter: Statementstmt 93.2% 83 / 89 Branchbran 81.8% 18 / 22 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 93.3% 14 / 15 Podpod n/a totaltotal 89.3% 117 / 131 CC 17 SCAR 15.0
CC
17
Cov
90%
import
8.1
__ANON__
7.0
__ANON__
3.0
CRAP 4.5
« Member.pm ↑ Summary Member/GLOB.pm »
1 package Class::Member::HASH;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 our $VERSION='1.6';
5  
6 2 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 my $get_class_members;
9 4 ▶$get_class_members=sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
10 4 my $pack=$_[0];
11 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 my %cm;
13 4 @cm{@{$pack.'::CLASS_MEMBERS'}}=();
14 4 foreach my $p (@{$pack.'::ISA'}) {
15 2 @cm{$get_class_members->($p)}=();
16 }
17 4 return keys %cm;
18 2 };
19  
20 3 ▶sub import {
Subroutine import: called CC=8 SCAR=20.9
21 3 my $pack=shift;
22 3 ($pack)=caller;
23  
24 26 ▶ my $getset=sub : lvalue {
Subroutine __ANON__: called CC=3 SCAR=11.0
25 26 my $I=shift;
26 26 my $what=shift;
27 26 ▶ unless( UNIVERSAL::isa( $I, 'HASH' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($I, "HASH")) 1 25 26
28 1 confess "$pack\::$what must be called as instance method\n";
29 }
30 25 $what=$pack.'::'.$what;
31 25 ▶ if( $#_>=0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ >= 0) 2 23 25
32 2 $I->{$what}=shift;
33 }
34 25 $I->{$what};
35 3 };
36  
37 2 ▶ my $new=sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
38 2 my $parent=shift;
39 2 ▶ my $class=ref($parent) || $parent;
Condition66%2/3
ref $parent || $parent
l!l&&r!l&&!r
110
MC/DC0%0/2
ref $parent || $parent
ref $parent $parent
Truth table66%2/3
A || B
A= ref $parentB= $parent
ABresult
1X1
011
000
40  
41 2 my $I=bless {}=>$class;
42 2 my %o=@_;
43 2 my @CLASS_MEMBERS=$get_class_members->($pack);
44  
45 2 ▶ if( ref($parent) ) { # inherit first
Branch: 1 branch
BranchTrueFalseTotal
if (ref $parent) 1 1 2
46 1 foreach my $m (@CLASS_MEMBERS) {
47 3 $I->$m=$parent->$m;
48 }
49 }
50  
51 # then override with named parameters
52 2 foreach my $m (@CLASS_MEMBERS) {
53 6 ▶ $I->$m=$o{$m} if( exists $o{$m} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $o{$m} 3 3 6
54 }
55  
56 2 my $init=$I->can('I N I T');
57 2 ▶ if( $init ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($init) 2 0 2
58 2 $init=$init->();
59 2 $I->$init;
60 }
61  
62 2 return $I;
63 3 };
64  
65 3 foreach my $name (@_) {
66 10 ▶ if( $name=~/^-(.*)/ ) { # reserved name, aka option
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^-(.*)/) { } 5 5 10
67 5 my $o=$1;
68 5 ▶ if( $o eq 'CLASS_MEMBERS' ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($o eq 'CLASS_MEMBERS') { } 3 2 5
elsif ($o =~ /^NEW=(.+)/) { } 1 1 2
elsif ($o eq 'NEW') { } 0 1 1
elsif ($o =~ /^INIT=(.+)/) { } 0 1 1
elsif ($o eq 'INIT') { } 1 0 1
69 3 local $_;
70 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
71 3 *{$pack.'::CLASS_MEMBERS'}=[grep {!/^-/} @_];
72 } elsif( $o=~/^NEW=(.+)/ ) {
73 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
74 1 *{$pack.'::'.$1}=$new;
75 } elsif( $o eq 'NEW' ) {
76 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
77 0 *{$pack.'::new'}=$new;
78 } elsif( $o=~/^INIT=(.+)/ ) {
79 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
80 0 my $init="$1";
81 0 ▶ *{$pack.'::I N I T'}=sub(){$init};
Subroutine __ANON__: not called CC=1 SCAR=6.9
82 } elsif( $o eq 'INIT' ) {
83 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
84 1 *{$pack.'::I N I T'}=sub(){"init"};
85 }
86 } else {
87 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
88 5 ▶ *{$pack.'::'.$name}=sub:lvalue {
Subroutine __ANON__: called CC=1 SCAR=0.0
89 26 my $I=shift;
90 26 &{$getset}( $I, $name, @_ );
91 5 };
92 }
93 }
94 }
95  
96 2 1;
97  
98 __END__
« Member.pm ↑ Summary Member/GLOB.pm »