blib/lib/Enumeration.pm

filter: Statementstmt 96.6% 87 / 90 Branchbran 86.3% 19 / 22 Conditioncond 52.3% 11 / 21 MC/DCmcdc 35.7% 5 / 14 Subroutinesub 95.8% 23 / 24 Podpod 69.2% 9 / 13 totaltotal 83.7% 154 / 184 CC 23 SCAR 12.0
CC
23
Cov
88%
_defeq
6.6
is_any
6.0
is_allowable_value
4.2
CRAP 3.3
↑ Summary
1 =for gpg
2 -----BEGIN PGP SIGNED MESSAGE-----
3 Hash: SHA1
4  
5 =head1 NAME
6  
7 Enumeration - Yet Another enumeration class implementation.
8  
9 =head1 VERSION
10  
11 This is version 0.03 of Enumeration, of March 26, 2008.
12  
13 =cut
14  
15 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 package Enumeration;
18 7 $Enumeration::VERSION = '0.03';
19  
20 7 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 7 ▶use overload '""' => \&value,
Subroutine BEGIN: called CC=1 SCAR=0.0
23 'eq' => \&equals,
24 7 'ne' => \&not_equals;
25  
26 # Auto-croaking saves program startup time:
27 9 ▶sub croak { require Carp; goto &Carp::croak }
Subroutine croak: called CC=1 SCAR=0.0
Pod undocumented
28  
29 # Enumeration constants for each subclass
30 7 my %class_symbols;
31  
32 # This should ONLY be called by subclasses.
33 # Call as: __PACKAGE__->set_enumerations(@list_of_symbols);
34 sub set_enumerations
35 7 ▶{
Subroutine set_enumerations: called CC=2 SCAR=6.9
Pod documented
36 7 my $class = shift;
37 7 $class_symbols{$class}{$_} = 1 for @_;
38 7 return 1;
39 }
40  
41 # Return a list of enumerations allowable in the given class.
42 sub enumerations
43 0 ▶{
Subroutine enumerations: not called CC=1 SCAR=6.9
Pod undocumented
44 0 my $class = shift;
45 0 return keys %{ $class_symbols{$class} };
46 }
47  
48 sub import
49 11 ▶{
Subroutine import: called CC=4 SCAR=13.9
50 11 my $class = shift;
51 11 ▶ my $import = @_ && $_[0] eq ':all';
Condition66%2/3
@_ && $_[0] eq ':all'
!ll&&!rl&&r
902
MC/DC50%1/2
@_ && $_[0] eq ':all'
@_ $_[0] eq ':all'
Truth table66%2/3
A && B
A= @_B= $_[0] eq ':all'
ABresult
0X0
100
111
52  
53 11 my $cpkg = caller;
54 11 foreach my $sym (keys %{$class_symbols{$class}})
55 {
56 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
57 31 my $full_name = $cpkg . '::' . $sym;
58 31 my $local_name = $class . '::' . $sym;
59  
60 31 ▶ *$full_name = sub () { $sym } if $import;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $import 11 20 31
61 16 ▶ *$local_name = sub () { $sym }
Subroutine __ANON__: called CC=1 SCAR=0.0
62 31 }
63 }
64  
65  
66 # OO enclosure.
67 {
68 # Enumeration constants for objects created directly from the Enumeration class.
69 7 my %instance_symbols;
70 7 my %instance_value;
71  
72 sub new
73 10 ▶ {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
74 10 my $class = shift;
75 10 my $self = bless \do { my $dummy } => $class;
76  
77 # Caller is creating an on-the-fly enumeration
78 10 ▶ if ($class eq 'Enumeration')
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq 'Enumeration') { } 3 7 10
79 {
80 3 my %values = map {$_ => 1} @_;
81 3 $instance_symbols{refaddr $self} = \%values;
82 }
83 else # Caller is using a subclass
84 {
85 7 ▶ croak "Too many arguments to ${class}->new" if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 7 7
86 7 $instance_symbols{refaddr $self} = $class_symbols{$class};
87 7 ▶ $self->set(shift) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 6 1 7
88 }
89  
90 10 return $self;
91 }
92  
93 sub DESTROY
94 10 ▶ {
Subroutine DESTROY: called CC=1 SCAR=0.0
95 10 my $self = shift;
96 10 delete $instance_symbols{refaddr $self};
97 10 delete $instance_value{refaddr $self};
98 }
99  
100 # Is a given value in the list of enumeration values that are legal
101 # for this class or object?
102 sub is_allowable_value
103 88 ▶ {
Subroutine is_allowable_value: called CC=4 SCAR=14.2
Pod documented
104 88 my $what = shift; # may be class name string or an object reference
105 88 my $value = shift;
106 88 ▶ return 1 if not defined $value; # undef is always allowed.
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 88 88
107  
108 # It's a "free" enum object -- instance contains the allowable values.
109 88 ▶ if (ref $what eq 'Enumeration')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $what eq "Enumeration") 38 50 88
110 {
111 38 return $instance_symbols{refaddr $what}{$value};
112 }
113  
114 # It's a subclass-based object -- enumeration is at the class level.
115 50 ▶ $what = ref ($what) || $what;
Condition33%1/3
ref $what || $what
l!l&&r!l&&!r
5000
MC/DC0%0/2
ref $what || $what
ref $what $what
Truth table33%1/3
A || B
A= ref $whatB= $what
ABresult
1X1
011
000
116 50 return $class_symbols{$what}{$value};
117 }
118  
119 # simple internal routine for generating a consistent error message
120 # throughout.
121 sub _check
122 88 ▶ {
Subroutine _check: called CC=2 SCAR=6.9
123 88 ▶ croak qq{"$_[1]" is not an allowable value}
Branch: 1 branch
BranchTrueFalseTotal
if not $_[0]->is_allowable_value($_[1]) 9 79 88
124 if not $_[0]->is_allowable_value($_[1]);
125 }
126  
127 # Set the object's value.
128 sub set
129 18 ▶ {
Subroutine set: called CC=1 SCAR=0.0
Pod documented
130 18 my $self = shift;
131 18 my $value = shift;
132  
133 18 $self->_check($value);
134 15 $instance_value{refaddr $self} = $value;
135 }
136  
137 # Return the object's value.
138 sub value
139 6 ▶ {
Subroutine value: called CC=1 SCAR=0.0
Pod documented
140 6 my $self = shift;
141 6 return ref($self) . '::' . $instance_value{refaddr $self}
142 }
143 sub bare_value
144 6 ▶ {
Subroutine bare_value: called CC=1 SCAR=0.0
Pod documented
145 6 my $self = shift;
146 6 return $instance_value{refaddr $self}
147 }
148  
149 # Query the object's status; check to see if it is a given value.
150 sub is
151 53 ▶ {
Subroutine is: called CC=4 SCAR=13.9
Pod documented
152 53 my $self = shift;
153 53 my $value = shift;
154  
155 # Comparing to another enum object?
156 53 ▶ if (ref $value && $value->isa('Enumeration'))
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value and $value->isa("Enumeration")) 11 42 53
Condition66%2/3
ref $value and $value->isa("Enumeration")
!ll&&!rl&&r
42011
MC/DC50%1/2
ref $value and $value->isa("Enumeration")
ref $value $value->isa("Enumeration")
Truth table66%2/3
A and B
A= ref $valueB= $value->isa("Enumeration")
ABresult
0X0
100
111
157 {
158 # Compatible classes? Equivalent values?
159 return ref $value eq ref $self &&
160 _defeq($instance_value{refaddr $self},
161 11 ▶ $instance_value{refaddr $value});
Condition100%3/3
ref $value eq ref $self && _defeq($instance_value{builtin::refaddr($self)}, $instance_value{builtin::refaddr($value)})
!ll&&!rl&&r
335
MC/DC100%2/2
ref $value eq ref $self && _defeq($instance_value{builtin::refaddr($self)}, $instance_value{builtin::refaddr($value)})
ref $value eq ref $self _defeq($instance_value{builtin::refaddr($self)}, $instance_value{builtin::refaddr($value)})
Truth table100%3/3
A && B
A= ref $value eq ref $selfB= _defeq($instance_value{builtin::refaddr($self)}, $instance_value{builtin::refaddr($value)})
ABresult
0X0
100
111
162 }
163  
164 42 $self->_check($value);
165 40 return _defeq($instance_value{refaddr $self}, $value);
166 }
167  
168 # "Complex" equality:
169 # If either value is undef, then they're equal only if both are undef
170 # Otherwise, just a simple string equality.
171 sub _defeq
172 48 ▶ {
Subroutine _defeq: called CC=4 SCAR=18.9
173 48 my ($v1, $v2) = @_;
174  
175 48 ▶ return (!defined $v1 && !defined $v2)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v1 and defined $v2 0 48 48
Condition16%1/6
defined $v1 and defined $v2
!ll&&!rl&&r
0048
defined $v1 || defined $v2
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $v1 and defined $v2
defined $v1 defined $v2
MC/DC0%0/2
defined $v1 || defined $v2
defined $v1 defined $v2
Truth table33%1/3
A and B
A= defined $v1B= defined $v2
ABresult
0X0
100
111
Truth table0%0/3
A || B
A= defined $v1B= defined $v2
ABresult
1X1
011
000
176 if (!defined $v1 || !defined $v2);
177  
178 48 return $v1 eq $v2;
179 }
180  
181 # Query the object's status; check to see if it is any of a number
182 # of possible values.
183 # Each status passed must be an allowable value.
184 sub is_any
185 14 ▶ {
Subroutine is_any: called CC=6 SCAR=17.9
Pod documented
186 14 my $self = shift;
187  
188 14 foreach my $value (@_)
189 {
190 34 ▶ next if ref $value && $value->isa('Enumeration');
Branch: 1 branch
BranchTrueFalseTotal
if ref $value and $value->isa("Enumeration") 6 28 34
Condition66%2/3
ref $value and $value->isa("Enumeration")
!ll&&!rl&&r
2806
MC/DC50%1/2
ref $value and $value->isa("Enumeration")
ref $value $value->isa("Enumeration")
Truth table66%2/3
A and B
A= ref $valueB= $value->isa("Enumeration")
ABresult
0X0
100
111
191 28 $self->_check($value);
192 }
193  
194 10 foreach my $value (@_)
195 {
196 26 ▶ return 1 if $self->is($value);
Branch: 1 branch
BranchTrueFalseTotal
if $self->is($value) 6 20 26
197 }
198 4 return;
199 }
200  
201 # Opposite of is_any. (Duh)
202 sub is_none
203 7 ▶ {
Subroutine is_none: called CC=1 SCAR=0.0
Pod documented
204 7 my $self = shift;
205 7 return ! $self->is_any(@_);
206 }
207  
208 # Overload methods
209  
210 sub equals
211 8 ▶ {
Subroutine equals: called CC=1 SCAR=0.0
Pod undocumented
212 8 return $_[0]->is($_[1]);
213 }
214  
215 sub not_equals
216 7 ▶ {
Subroutine not_equals: called CC=1 SCAR=0.0
Pod undocumented
217 7 return ! $_[0]->is($_[1]);
218 }
219  
220 }
221  
222  
223 7 return 'a true value';
224 __END__
↑ Summary