AccessorMaker/Private.pm

filter: Statementstmt 82.0% 41 / 50 Branchbran 63.6% 14 / 22 Conditioncond 35.7% 5 / 14 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 85.7% 6 / 7 Podpod n/a totaltotal 65.6% 67 / 102 CC 18 SCAR 21.8
CC
18
Cov
70%
import
14.3
__ANON__
10.1
__ANON__
5.1
CRAP 8.9
↑ Summary AccessorMaker.pm »
1 package Class::AccessorMaker::Private;
2  
3 ##
4 ## Class::AccessorMaker::Private by Hartog 'Sinister' de Mik
5 ##
6  
7 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 our $VERSION = "1.0";
11  
12 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # lexical for data-hiding
15  
16 3 my %obj_ = ();
17  
18 3 ▶sub import {
Subroutine import: called CC=10 SCAR=26.6
19 3 my ($class, $subs, $xtra) = @_;
20 3 ▶ my $pkg = ref(caller) || caller;
Condition33%1/3
ref caller || caller
l!l&&r!l&&!r
030
MC/DC0%0/2
ref caller || caller
ref caller caller
Truth table33%1/3
A || B
A= ref callerB= caller
ABresult
1X1
011
000
21 3 ▶ $xtra ||="";
Condition100%2/2
$xtra ||= ""
l!l
21
MC/DC100%1/1
$xtra ||= ""
$xtra
Truth table100%2/2
A
A= $xtra
Aresult
11
00
22  
23 3 ▶ croak "Can't make methods out of an empty hash-ref\n" if !defined $subs;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $subs 0 3 3
24  
25 ## define a constructor
26 3 ▶ if ( $xtra ne "no_new" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xtra ne "no_new") 2 1 3
27 # we have a green light for 'new()' creation
28 2 ▶ if ( $xtra ne "new_init" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($xtra ne 'new_init') { } 1 1 2
elsif ($xtra eq 'new_init') { } 1 0 1
29 1 ▶ *{"${pkg}::new"} = sub {
Subroutine __ANON__: not called CC=3 SCAR=23.1
30 0 ▶ my $class = ref($_[0]) || $_[0]; shift;
Condition0%0/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table0%0/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
31 0 my $self = bless({}, $class);
32  
33 0 ▶ while ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
34 0 my ($sub, $value) = (shift, shift);
35 0 $self->$sub($value);
36 }
37  
38 0 return $self;
39 1 };
40 } elsif ( $xtra eq "new_init" ) {
41 1 ▶ *{"${pkg}::new"} = sub {
Subroutine __ANON__: called CC=3 SCAR=12.3
42 1 ▶ my $class = ref($_[0]) || $_[0]; shift;
Condition33%1/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table33%1/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
43 1 my $self = bless({}, $class);
44  
45 1 ▶ while ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 1 1
46 0 my ($sub, $value) = (shift, shift);
47 0 $self->$sub($value);
48 }
49  
50 1 $self->init();
51 1 return $self;
52 1 };
53 }
54 }
55
56  
57 3 foreach my $sub ( keys %$subs ) {
58 # construct the method if it is not defined yet.
59 6 ▶ next if ( defined &{"${pkg}::$sub"} );
Branch: 1 branch
BranchTrueFalseTotal
if defined &{"${pkg}::$sub";} 0 6 6
60  
61 6 ▶ *{"${pkg}::$sub"} = sub {
Subroutine __ANON__: called CC=5 SCAR=16.4
62 4 my ( $self, $value ) = @_;
63  
64 # fill with default at first run
65 4 ▶ $obj_{$self}->{$sub} = $subs->{$sub} if !exists $obj_{$self}->{$sub};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $obj_{$self}{$sub} 1 3 4
66  
67 # more then just self, something has to be set.
68 4 ▶ if ($#_ > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ > 0) 1 3 4
69 warn "The value supplied to '$sub()' is not of propper type"
70 1 ▶ if (ref($subs->{$sub}) and !ref($value));
Branch: 1 branch
BranchTrueFalseTotal
if ref $subs->{$sub} and not ref $value 0 1 1
Condition33%1/3
ref $subs->{$sub} and not ref $value
!ll&&!rl&&r
100
MC/DC0%0/2
ref $subs->{$sub} and not ref $value
ref $subs->{$sub} not ref $value
Truth table33%1/3
A and not(B)
A= ref $subs->{$sub}B= not ref $value
ABresult
0X0
100
111
71
72 # set the value and return the object.
73 1 $obj_{$self}->{$sub} = $value;
74 1 return $self;
75 }
76  
77 # return the value;
78 3 return $obj_{$self}->{$sub};
79
80 6 ▶ } or warn "Method: $sub not implemented\n";
Branch: 1 branch
BranchTrueFalseTotal
*{"${pkg}::$sub";} = sub { my($self, $value) = @_; $obj_{$self}{$sub} = $subs->{$sub} unless exists $obj_{$self}{$sub}; if ($#_ > 0) { warn "The value supplied to '$sub()' is not of propper type" if ref $subs->{$sub} and not ref $value; $obj_{$self}{$sub} = $value; return $self; } ; return $obj_{$self}{$sub}; } or warn "Method: $sub not implemented\n" 6 0 6
81 }
82 3 return 1; # import succeeded
83 }
84  
85  
86 3 1;
87  
88 __END__
↑ Summary AccessorMaker.pm »