AccessorMaker.pm

filter: Statementstmt 95.0% 57 / 60 Branchbran 75.0% 21 / 28 Conditioncond 50.0% 7 / 14 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 78.8% 93 / 118 CC 21 SCAR 19.7
CC
21
Cov
83%
import
10.7
__ANON__
8.1
__ANON__
3.4
CRAP 7.2
« AccessorMaker/Private.pm ↑ Summary
1 package Class::AccessorMaker;
2  
3 ##
4 ## Class::AccessorMaker by Hartog "Sinister" de Mik
5 ##
6  
7 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 our $VERSION = "1.11";
11  
12 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 ▶sub import {
Subroutine import: called CC=10 SCAR=23.7
15 6 my ($class, $subs, $xtra) = @_;
16 6 ▶ my $pkg = ref(caller) || caller;
Condition33%1/3
ref caller || caller
l!l&&r!l&&!r
060
MC/DC0%0/2
ref caller || caller
ref caller caller
Truth table33%1/3
A || B
A= ref callerB= caller
ABresult
1X1
011
000
17 6 ▶ $xtra ||="";
Condition100%2/2
$xtra ||= ""
l!l
24
MC/DC100%1/1
$xtra ||= ""
$xtra
Truth table100%2/2
A
A= $xtra
Aresult
11
00
18  
19 6 ▶ croak "Can't make methods out of an empty hash-ref\n" if !defined $subs;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $subs 0 6 6
20  
21 ## define a constructor
22 6 ▶ if ( $xtra ne "no_new" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xtra ne "no_new") 5 1 6
23 # we have a green light for 'new()' creation
24 5 ▶ if ( $xtra ne "new_init" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($xtra ne 'new_init') { } 4 1 5
elsif ($xtra eq 'new_init') { } 1 0 1
25 4 ▶ *{"${pkg}::new"} = sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
26 4 ▶ my $class = ref($_[0]) || $_[0]; shift;
Condition33%1/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
040
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
27 4 my $self = bless({}, $class);
28  
29 4 ▶ while ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 4 7
30 3 my ($sub, $value) = (shift, shift);
31 3 $self->$sub($value);
32 }
33  
34 4 return $self;
35 4 };
36 } elsif ( $xtra eq "new_init" ) {
37 1 ▶ *{"${pkg}::new"} = sub {
Subroutine __ANON__: called CC=3 SCAR=12.3
38 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
39 1 my $self = bless({}, $class);
40  
41 1 ▶ while ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 1 1
42 0 my ($sub, $value) = (shift, shift);
43 0 $self->$sub($value);
44 }
45  
46 1 $self->init();
47 1 return $self;
48 1 };
49 }
50 }
51
52  
53 6 foreach my $sub ( keys %$subs ) {
54 ## construct the method if it is not defined yet.
55 13 ▶ next if ( defined &{"${pkg}::$sub"} );
Branch: 1 branch
BranchTrueFalseTotal
if defined &{"${pkg}::$sub";} 0 13 13
56  
57 13 ▶ *{"${pkg}::$sub"} = sub {
Subroutine __ANON__: called CC=8 SCAR=20.9
58 25 my ( $self, $value ) = @_;
59  
60 # fill with default at first run
61 25 ▶ if ( !exists $self->{$sub} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{$sub}) 12 13 25
62 12 my $val = $subs->{$sub};
63 12 my $rval = ref($val);
64 12 ▶ if (!$rval) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $rval) { } 6 6 12
elsif ($rval =~ /ARRAY/) { } 3 3 6
elsif ($rval =~ /HASH/) { } 3 0 3
65 #scalar
66 6 $self->{$sub} = $val;
67 } elsif ($rval =~ /ARRAY/) {
68 3 my @val = @{$val};
69 3 $self->{$sub} = \@val;
70 } elsif($rval =~ /HASH/) {
71 3 my %val = %{$val};
72 3 $self->{$sub} = \%val;
73 } else {
74 #object ref.. use at own risk.
75 0 $self->{$sub} = $val;
76 }
77 }
78  
79 # more then just self, something has to be set.
80 25 ▶ if ($#_ > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ > 0) 10 15 25
81 warn "The value supplied to '$sub()' is not of propper type"
82 10 ▶ if (ref($subs->{$sub}) and !ref($value));
Branch: 1 branch
BranchTrueFalseTotal
if ref $subs->{$sub} and not ref $value 0 10 10
Condition66%2/3
ref $subs->{$sub} and not ref $value
!ll&&!rl&&r
460
MC/DC0%0/2
ref $subs->{$sub} and not ref $value
ref $subs->{$sub} not ref $value
Truth table66%2/3
A and not(B)
A= ref $subs->{$sub}B= not ref $value
ABresult
0X0
100
111
83
84 # set the value and return the object.
85 10 $self->{$sub} = $value;
86 10 return $self;
87 }
88  
89 # return the value;
90 15 return $self->{$sub};
91
92 13 ▶ } or warn "Method: $sub not implemented\n";
Branch: 1 branch
BranchTrueFalseTotal
*{"${pkg}::$sub";} = sub { my($self, $value) = @_; unless (exists $self->{$sub}) { my $val = $subs->{$sub}; my $rval = ref $val; if (not $rval) { $self->{$sub} = $val; } elsif ($rval =~ /ARRAY/) { my(@val) = @{$val;}; $self->{$sub} = \@val; } elsif ($rval =~ /HASH/) { my(%val) = %{$val;}; $self->{$sub} = \%val; } else { $self->{$sub} = $val; } ; } ; if ($#_ > 0) { warn "The value supplied to '$sub()' is not of propper type" if ref $subs->{$sub} and not ref $value; $self->{$sub} = $value; return $self; } ; return $self->{$sub}; } or warn "Method: $sub not implemented\n" 13 0 13
93 }
94 6 return 1; # import succeeded
95 }
96  
97  
98 4 1;
99  
100 __END__
« AccessorMaker/Private.pm ↑ Summary