fields.pm

filter: Statementstmt 49.5% 54 / 109 Branchbran 35.4% 17 / 48 Conditioncond 76.9% 10 / 13 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 66.6% 8 / 12 Podpod 50.0% 1 / 2 totaltotal 49.7% 95 / 191 CC 31 SCAR 34.6
CC
31
Cov
48%
_dump
72.0
phash
61.9
import
15.9
CRAP 31.8
« base.pm ↑ Summary
1 6 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package fields;
3  
4 6 require 5.005;
5 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶unless( eval q{require warnings::register; warnings::register->import; 1} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval "require warnings::register; warnings::register->import; 1") 0 6 6
8 0 ▶ *warnings::warnif = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
9 0 require Carp;
10 0 Carp::carp(@_);
11 }
12 0 }
13 6 ▶use vars qw(%attr $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 6 $VERSION = '2.23';
16 6 $VERSION =~ tr/_//d;
17  
18 # constant.pm is slow
19 sub PUBLIC () { 2**0 }
20 sub PRIVATE () { 2**1 }
21 sub INHERITED () { 2**2 }
22 sub PROTECTED () { 2**3 }
23  
24  
25 # The %attr hash holds the attributes of the currently assigned fields
26 # per class. The hash is indexed by class names and the hash value is
27 # an array reference. The first element in the array is the lowest field
28 # number not belonging to a base class. The remaining elements' indices
29 # are the field numbers. The values are integer bit masks, or undef
30 # in the case of base class private fields (which occupy a slot but are
31 # otherwise irrelevant to the class).
32  
33 56 ▶sub import {
Subroutine import: called CC=15 SCAR=27.7
34 56 my $class = shift;
35 56 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 2 54 56
36 54 my $package = caller(0);
37 # avoid possible typo warnings
38 54 ▶ %{"$package\::FIELDS"} = () unless %{"$package\::FIELDS"};
Branch: 1 branch
BranchTrueFalseTotal
unless %{"${package}::FIELDS";} 25 29 54
39 54 my $fields = \%{"$package\::FIELDS"};
40 54 ▶ my $fattr = ($attr{$package} ||= [1]);
Condition100%2/2
$fields::attr{$package} ||= [1]
l!l
3123
MC/DC100%1/1
$fields::attr{$package} ||= [1]
$fields::attr{$package}
Truth table100%2/2
A
A= $fields::attr{$package}
Aresult
11
00
41 54 my $next = @$fattr;
42  
43 # Quiet pseudo-hash deprecation warning for uses of fields::new.
44 54 bless \%{"$package\::FIELDS"}, 'pseudohash';
45  
46 54 ▶ if ($next > $fattr->[0]
Branch: 1 branch
BranchTrueFalseTotal
if ($next > $fattr->[0] and ($$fields{$_[0]} || 0) >= $fattr->[0]) 6 48 54
Condition100%5/5
$next > $fattr->[0] and ($$fields{$_[0]} || 0) >= $fattr->[0]
!ll&&!rl&&r
4176
$$fields{$_[0]} || 0
l!l
67
MC/DC100%2/2
$next > $fattr->[0] and ($$fields{$_[0]} || 0) >= $fattr->[0]
$next > $fattr->[0] $$fields{$_[0]}
Truth table100%3/3
A and B
A= $next > $fattr->[0]B= $$fields{$_[0]}
ABresult
0X0
100
111
47 and ($fields->{$_[0]} || 0) >= $fattr->[0])
48 {
49 # There are already fields not belonging to base classes.
50 # Looks like a possible module reload...
51 6 $next = $fattr->[0];
52 }
53 54 foreach my $f (@_) {
54 132 my $fno = $fields->{$f};
55  
56 # Allow the module to be reloaded so long as field positions
57 # have not changed.
58 132 ▶ if ($fno and $fno != $next) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fno and $fno != $next) 3 129 132
Condition100%3/3
$fno and $fno != $next
!ll&&!rl&&r
108213
MC/DC100%2/2
$fno and $fno != $next
$fno $fno != $next
Truth table100%3/3
A and B
A= $fnoB= $fno != $next
ABresult
0X0
100
111
59 3 require Carp;
60 3 ▶ if ($fno < $fattr->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fno < $fattr->[0]) { } 3 0 3
61 3 ▶ if ($] < 5.006001) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] < 5.006001) { } 0 3 3
62 0 ▶ warn("Hides field '$f' in base class") if $^W;
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
63 } else {
64 3 warnings::warnif("Hides field '$f' in base class") ;
65 }
66 } else {
67 0 Carp::croak("Field name '$f' already in use");
68 }
69 }
70 132 $fields->{$f} = $next;
71 132 ▶ $fattr->[$next] = ($f =~ /^_/) ? PRIVATE : PUBLIC;
Branch: 1 branch
BranchTrueFalseTotal
$f =~ /^_/ ? : 36 96 132
72 132 $next += 1;
73 }
74 54 ▶ if (@$fattr > $next) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$fattr > $next) 0 54 54
75 # Well, we gave them the benefit of the doubt by guessing the
76 # module was reloaded, but they appear to be declaring fields
77 # in more than one place. We can't be sure (without some extra
78 # bookkeeping) that the rest of the fields will be declared or
79 # have the same positions, so punt.
80 0 require Carp;
81 0 Carp::croak ("Reloaded module must declare all fields at once");
82 }
83 }
84  
85 0 ▶sub inherit {
Subroutine inherit: not called CC=1 SCAR=6.9
Pod undocumented
86 0 require base;
87 0 goto &base::inherit_fields;
88 }
89  
90 sub _dump # sometimes useful for debugging
91 0 ▶{
Subroutine _dump: not called CC=8 SCAR=42.8
92 0 for my $pkg (sort keys %attr) {
93 0 print "\n$pkg";
94 0 ▶ if (@{"$pkg\::ISA"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{"${pkg}::ISA";}) 0 0 0
95 0 print " (", join(", ", @{"$pkg\::ISA"}), ")";
96 }
97 0 print "\n";
98 0 my $fields = \%{"$pkg\::FIELDS"};
99 0 for my $f (sort {$fields->{$a} <=> $fields->{$b}} keys %$fields) {
100 0 my $no = $fields->{$f};
101 0 print " $no: $f";
102 0 my $fattr = $attr{$pkg}[$no];
103 0 ▶ if (defined $fattr) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fattr) 0 0 0
104 0 my @a;
105 0 ▶ push(@a, "public") if $fattr & PUBLIC;
Branch: 1 branch
BranchTrueFalseTotal
if $fattr & 1 0 0 0
106 0 ▶ push(@a, "private") if $fattr & PRIVATE;
Branch: 1 branch
BranchTrueFalseTotal
if $fattr & 2 0 0 0
107 0 ▶ push(@a, "inherited") if $fattr & INHERITED;
Branch: 1 branch
BranchTrueFalseTotal
if $fattr & 4 0 0 0
108 0 print "\t(", join(", ", @a), ")";
109 }
110 0 print "\n";
111 }
112 }
113 }
114  
115 6 ▶if ($] < 5.009) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] < 5.009) { } 0 6 6
116 0 ▶ *new = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
117 0 my $class = shift;
118 0 ▶ $class = ref $class if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 0 0
119 0 return bless [\%{$class . "::FIELDS"}], $class;
120 }
121 0 } else {
122 7 ▶ *new = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
123 7 my $class = shift;
124 7 ▶ $class = ref $class if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 7 7
125 7 require Hash::Util;
126 7 my $self = bless {}, $class;
127  
128 # The lock_keys() prototype won't work since we require Hash::Util :(
129 7 &Hash::Util::lock_keys(\%$self, _accessible_keys($class));
130 7 return $self;
131 }
132 6 }
133  
134 10 ▶sub _accessible_keys {
Subroutine _accessible_keys: called CC=1 SCAR=0.0
135 10 my ($class) = @_;
136 return (
137 10 keys %{$class.'::FIELDS'},
138 10 map(_accessible_keys($_), @{$class.'::ISA'}),
139 );
140 }
141  
142 1 ▶sub phash {
Subroutine phash: called CC=8 SCAR=41.3
Pod documented
143 1 ▶ die "Pseudo-hashes have been removed from Perl" if $] >= 5.009;
Branch: 1 branch
BranchTrueFalseTotal
if $] >= "5.009" 1 0 1
144 0 my $h;
145 my $v;
146 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
147 0 ▶ if (ref $_[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'ARRAY') { } 0 0 0
148 0 my $a = shift;
149 0 @$h{@$a} = 1 .. @$a;
150 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
151 0 $v = shift;
152 0 ▶ unless (! @_ and ref $v eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (not @_ and ref $v eq "ARRAY") 0 0 0
Condition0%0/3
not @_ and ref $v eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
not @_ and ref $v eq "ARRAY"
not @_ ref $v eq "ARRAY"
Truth table0%0/3
not(A) and B
A= not @_B= ref $v eq "ARRAY"
ABresult
0X0
100
111
153 0 require Carp;
154 0 Carp::croak ("Expected at most two array refs\n");
155 }
156 }
157 }
158 else {
159 0 ▶ if (@_ % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2) 0 0 0
160 0 require Carp;
161 0 Carp::croak ("Odd number of elements initializing pseudo-hash\n");
162 }
163 0 my $i = 0;
164 0 @$h{grep ++$i % 2, @_} = 1 .. @_ / 2;
165 0 $i = 0;
166 0 $v = [grep $i++ % 2, @_];
167 }
168 }
169 else {
170 0 $h = {};
171 0 $v = [];
172 }
173 0 [ $h, @$v ];
174  
175 }
176  
177 6 1;
178  
179 __END__
« base.pm ↑ Summary