blib/lib/Class/Simple.pm

filter: Statementstmt 95.9% 188 / 196 Branchbran 81.7% 67 / 82 Conditioncond 87.5% 7 / 8 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 33 / 33 Podpod 100% 5 / 5 totaltotal 92.4% 304 / 329 CC 48 SCAR 17.2
CC
48
Cov
92%
AUTOLOAD
14.3
__ANON__
10.4
_travel_isa
7.0
CRAP 5.6
↑ Summary
1 #$Id: Simple.pm,v 1.29 2008/01/01 16:34:15 sullivan Exp $
2 #
3 # See the POD documentation starting towards the __END__ of this file.
4  
5 package Class::Simple;
6  
7 11 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 11 our $VERSION = '1.1';
12  
13 11 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 ## no critic
16 11 ▶use Class::ISA;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 ## use critic
18 11 ▶use List::Util qw( first );
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 11 my %STORAGE;
21 my %PRIVATE;
22 11 my %READONLY;
23 11 my @internal_attributes = qw(CLASS);
24  
25 11 our $AUTOLOAD;
26  
27 sub AUTOLOAD
28 97 ▶{
Subroutine AUTOLOAD: called CC=14 SCAR=26.6
29 97 my $self = shift;
30 97 my @args = @_;
31  
32 11 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 97 $AUTOLOAD =~ /(.*)::((get|set|clear|raise|readonly)_)?(\w+)/;
35 97 my $pkg = $1;
36 97 my $full_method = $AUTOLOAD;
37 97 ▶ my $prefix = $3 || '';
Condition100%2/2
$3 || ''
l!l
6829
MC/DC100%1/1
$3 || ''
$3
Truth table100%2/2
A
A= $3
Aresult
11
00
38 97 my $attrib = $4;
39 97 ▶ $prefix = '' if ($attrib =~ /^_/);
Branch: 1 branch
BranchTrueFalseTotal
if $attrib =~ /^_/ 11 86 97
40 97 my $store_as = $attrib;
41 97 ▶ $store_as =~ s/^_// unless $prefix;
Branch: 1 branch
BranchTrueFalseTotal
unless $prefix 35 62 97
42  
43 97 ▶ if (my $get_attributes = $self->can('ATTRIBUTES'))
Branch: 1 branch
BranchTrueFalseTotal
if (my $get_attributes = $self->can("ATTRIBUTES")) 12 85 97
44 {
45 12 my @attributes = &$get_attributes();
46 12 push(@attributes, @internal_attributes);
47 croak("$attrib is not a defined attribute in $pkg")
48 12 ▶ unless first {$_ eq $attrib} @attributes;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless &first(sub { $_ eq $attrib; } , @attributes) 2 10 12
49 }
50  
51 #
52 # Make sure that if you add more special prefixes here,
53 # you add them to the $AUTOLOAD regex above, too.
54 #
55 95 ▶ if ($prefix eq 'set')
Branch: 6 branches
BranchTrueFalseTotal
if ($prefix eq 'set') { } 24 71 95
elsif ($prefix eq 'get') { } 22 49 71
elsif ($prefix eq 'clear') { } 1 48 49
elsif ($prefix eq 'raise') { } 1 47 48
elsif ($prefix eq 'readonly') { } 13 34 47
elsif (not $prefix and $attrib =~ /^_/) { } 11 23 34
56 {
57 24 *{$AUTOLOAD} = sub
58 53 ▶ {
Subroutine __ANON__: called CC=5 SCAR=23.4
59 53 my $self = shift;
60  
61 53 my $ref = refaddr($self);
62 croak("$attrib is readonly: cannot set.")
63 53 ▶ if ($READONLY{$ref}->{$store_as});
Branch: 1 branch
BranchTrueFalseTotal
if $READONLY{$ref}{$store_as} 2 51 53
64 51 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) { } 0 51 51
65 # Must be initialized (in new or init) as an array ref or hash ref.
66 0 ▶ if (ref $STORAGE{$ref}->{$store_as} eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $STORAGE{$ref}{$store_as} eq 'ARRAY') { } 0 0 0
elsif (ref $STORAGE{$ref}{$store_as} eq 'HASH') { } 0 0 0
67 0 @{$STORAGE{$ref}->{$store_as}}[$_[0]] = $_[1];
68 0 return (@{$STORAGE{$ref}->{$store_as}}[$_[0]]);
69 } elsif (ref $STORAGE{$ref}->{$store_as} eq 'HASH') {
70 0 $STORAGE{$ref}->{$store_as}->{$_[0]} = $_[1];
71 0 return ($STORAGE{$ref}->{$store_as}->{$_[0]});
72 }
73 } else {
74 51 return ($STORAGE{$ref}->{$store_as} = shift(@_));
75 }
76 24 };
77 }
78 elsif ($prefix eq 'get')
79 {
80 22 *{$AUTOLOAD} = sub
81 67 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
82 67 my $self = shift;
83  
84 67 my $ref = refaddr($self);
85 67 return ($STORAGE{$ref}->{$store_as});
86 22 };
87 }
88 #
89 # Bug #7528 in Perl keeps this from working.
90 # http://rt.perl.org/rt3/Public/Bug/Display.html?id=7528
91 # I could make people declare methods they want to use lv_
92 # with but that goes against the philosophy of being ::Simple.
93 #
94 # elsif ($prefix eq 'lv')
95 # {
96 # *{$AUTOLOAD} = sub : lvalue
97 # {
98 # my $self = shift;
99 #
100 # my $ref = refaddr($self);
101 # croak("$attrib is readonly: cannot set.")
102 # if ($READONLY{$ref}->{$store_as});
103 # return ($STORAGE{$ref}->{$store_as});
104 # };
105 # }
106 elsif ($prefix eq 'clear')
107 {
108 1 my $setter = "set_$attrib";
109 1 *{$AUTOLOAD} = sub
110 2 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
111 2 my $self = shift;
112  
113 2 return ($self->$setter(undef));
114 1 };
115 }
116 elsif ($prefix eq 'raise')
117 {
118 1 my $setter = "set_$attrib";
119 1 *{$AUTOLOAD} = sub
120 2 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
121 2 my $self = shift;
122  
123 2 return ($self->$setter(1));
124 1 };
125 }
126 elsif ($prefix eq 'readonly')
127 {
128 13 my $setter = "set_$attrib";
129 13 *{$AUTOLOAD} = sub
130 24 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
131 24 my $self = shift;
132  
133 24 my $ret = $self->$setter(@_);
134 24 my $ref = refaddr($self);
135 24 $READONLY{$ref}->{$store_as} = 1;
136 24 return ($ret);
137 13 };
138 }
139 #
140 # All methods starting with '_' can only be called from
141 # within their package. Not inheritable, which makes
142 # the test easier than something privatized..
143 #
144 # Note that we cannot just call get_ and set_ here
145 # because if someone writes their own get_foo and then
146 # _foo is called, _foo will call set_foo, which will
147 # probably store something with _foo, which will call
148 # set_foo, etc. Sure wish we could somehow share
149 # code with get_ and set_, though.
150 #
151 ▶ elsif (!$prefix && ($attrib =~ /^_/))
Condition66%2/3
not $prefix and $attrib =~ /^_/
!ll&&!rl&&r
02311
MC/DC50%1/2
not $prefix and $attrib =~ /^_/
not $prefix $attrib =~ /^_/
Truth table66%2/3
not(A) and B
A= not $prefixB= $attrib =~ /^_/
ABresult
0X0
100
111
152 {
153 11 ▶ if (my $method = $pkg->can($attrib))
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = $pkg->can($attrib)) 3 8 11
154 {
155 3 return &$method($self, @args);
156 }
157  
158 8 *{$AUTOLOAD} = sub
159 18 ▶ {
Subroutine __ANON__: called CC=4 SCAR=13.9
160 18 my $self = shift;
161  
162 18 ▶ croak("Cannot call $attrib: Private method to $pkg.")
Branch: 1 branch
BranchTrueFalseTotal
unless $pkg->isa(_my_caller()) 4 14 18
163 unless ($pkg->isa(Class::Simple::_my_caller()));
164 14 my $ref = refaddr($self);
165 14 ▶ if (scalar(@_))
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) { } 6 8 14
166 {
167 croak("$attrib is readonly: cannot set.")
168 6 ▶ if ($READONLY{$ref}->{$store_as});
Branch: 1 branch
BranchTrueFalseTotal
if $READONLY{$ref}{$store_as} 0 6 6
169 6 return ($STORAGE{$ref}->{$store_as} =shift(@_));
170 }
171 else
172 {
173 8 return ($STORAGE{$ref}->{$store_as});
174 }
175 8 };
176 }
177 else
178 {
179 23 my $setter = "set_$store_as";
180 23 my $getter = "get_$store_as";
181 23 *{$AUTOLOAD} = sub
182 74 ▶ {
Subroutine __ANON__: called CC=2 SCAR=6.9
183 74 my $self = shift;
184  
185 74 ▶ return (scalar(@_)
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ ? : 11 63 74
186 ? $self->$setter(@_)
187 : $self->$getter());
188 23 };
189 }
190 92 return (&{$AUTOLOAD}($self, @args));
191 }
192  
193  
194  
195 #
196 # Call all the DEMOLISH()es and then delete from %STORAGE.
197 #
198 sub DESTROY
199 22 ▶{
Subroutine DESTROY: called CC=3 SCAR=11.0
200 22 my $self = shift;
201  
202 22 $self->_travel_isa('DESTROY', 'DEMOLISH');
203 22 my $ref = refaddr($self);
204 22 ▶ delete($STORAGE{$ref}) if exists($STORAGE{$ref});
Branch: 1 branch
BranchTrueFalseTotal
if exists $STORAGE{$ref} 22 0 22
205 22 ▶ delete($READONLY{$ref}) if exists($READONLY{$ref});
Branch: 1 branch
BranchTrueFalseTotal
if exists $READONLY{$ref} 21 1 22
206 }
207  
208  
209  
210 #
211 # Travel up the class's @ISA and run $func, if we can.
212 # To keep from running a sub more than once we flag
213 # $storage in %STORAGE.
214 #
215 sub _travel_isa
216 42 ▶{
Subroutine _travel_isa: called CC=7 SCAR=19.5
217 42 my $self = shift;
218 42 my $storage = shift;
219 42 my $func = shift;
220  
221 42 my $ref = refaddr($self);
222 42 ▶ $STORAGE{$ref}->{$storage}= {} unless exists($STORAGE{$ref}->{$storage});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $STORAGE{$ref}{$storage} 42 0 42
223 42 my @path = reverse(Class::ISA::super_path($self->CLASS));
224 42 foreach my $c (@path)
225 {
226 60 ▶ next if ($c eq __PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if $c eq __PACKAGE__ 40 20 60
227 20 ▶ next if $STORAGE{$ref}->{$storage}->{$c}++;
Branch: 1 branch
BranchTrueFalseTotal
if $STORAGE{$ref}{$storage}{$c}++ 0 20 20
228  
229 20 my $cn = "${c}::can";
230 20 ▶ if (my $in = $c->can($func))
Branch: 1 branch
BranchTrueFalseTotal
if (my $in = $c->can($func)) 7 13 20
231 {
232 7 $self->$in(@_);
233 }
234 }
235 42 ▶ $self->$func(@_) if $self->can($func);;
Branch: 1 branch
BranchTrueFalseTotal
if $self->can($func) 11 31 42
236 }
237  
238  
239  
240 #
241 # Make a scalar. Bless it. Call init.
242 #
243 sub new
244 22 ▶{
Subroutine new: called CC=2 SCAR=6.9
Pod documented
245 22 my $class = shift;
246  
247 #
248 # Support for NONEW.
249 #
250 {
251 11 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
252 22 my $classy = "${class}::";
253 croak("Cannot call new() in $class.")
254 22 ▶ if exists(${$classy}{'NONEW'});
Branch: 1 branch
BranchTrueFalseTotal
if exists do { $classy }->{"NONEW"} 1 21 22
255 }
256  
257 #
258 # This is how you get an anonymous scalar.
259 #
260 21 my $self = \do{my $anon_scalar};
261 21 bless($self, $class);
262 21 $self->readonly_CLASS($class);
263  
264 21 $self->init(@_);
265 20 return ($self);
266 }
267  
268  
269  
270 #
271 # Flag the given method(s) as being private to the class
272 # (and its children unless overridden).
273 #
274 sub privatize
275 8 ▶{
Subroutine privatize: called CC=6 SCAR=17.9
Pod documented
276 8 my $class = shift;
277  
278 8 foreach my $method (@_)
279 {
280 11 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
281  
282 #
283 # Can't privatize something that is already private
284 # from an ancestor.
285 #
286 9 foreach my $private_class (keys(%PRIVATE))
287 {
288 7 ▶ next unless $PRIVATE{$private_class}->{$method};
Branch: 1 branch
BranchTrueFalseTotal
unless $PRIVATE{$private_class}{$method} 6 1 7
289 1 ▶ croak("Cannot privatize ${class}::$method: already private in $private_class.")
Branch: 1 branch
BranchTrueFalseTotal
unless $private_class->isa($class) 1 0 1
290 unless $private_class->isa($class);
291 }
292  
293 #
294 # Can't retroactively make privatize something.
295 #
296 8 my $called_by = _my_caller();
297 8 ▶ croak("Attempt to privatize ${class}::$method from $called_by. Can only privatize in your own class.")
Branch: 1 branch
BranchTrueFalseTotal
if $class ne $called_by 1 7 8
298 if ($class ne $called_by);
299 7 $PRIVATE{$class}->{$method} = 1;
300  
301 #
302 # Although it is duplication of code (which I hope
303 # to come up with a clever way to avoid at some point),
304 # it is a better solution to have privatize() create
305 # these subs now. Otherwise, having the private test
306 # done in AUTOLOAD gets to be fairly convoluted.
307 # Defining them here makes the tests a lot simpler.
308 #
309 7 my $getter = "${class}::get_$method";
310 7 my $setter = "${class}::set_$method";
311 7 my $generic = "${class}::$method";
312 7 *{$getter} = sub
313 1 ▶ {
Subroutine __ANON__: called CC=2 SCAR=7.0
314 1 my $self = shift;
315  
316 11 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
317 1 ▶ croak("Cannot call $getter: Private method to $class.")
Branch: 1 branch
BranchTrueFalseTotal
unless $class->isa(_my_caller()) 0 1 1
318 unless $class->isa(Class::Simple::_my_caller());
319 1 my $ref = refaddr($self);
320 1 return ($STORAGE{$ref}->{$method});
321 7 };
322 *$setter = sub
323 5 ▶ {
Subroutine __ANON__: called CC=3 SCAR=11.0
324 5 my $self = shift;
325  
326 11 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
327 5 ▶ croak("Cannot call $setter: Private method to $class.")
Branch: 1 branch
BranchTrueFalseTotal
unless $class->isa(_my_caller()) 0 5 5
328 unless $class->isa(Class::Simple::_my_caller());
329 5 my $ref = refaddr($self);
330 croak("$method is readonly: cannot set.")
331 5 ▶ if ($READONLY{$ref}->{$method});
Branch: 1 branch
BranchTrueFalseTotal
if $READONLY{$ref}{$method} 1 4 5
332 4 return ($STORAGE{$ref}->{$method} = shift(@_));
333 7 };
334 *$generic = sub
335 6 ▶ {
Subroutine __ANON__: called CC=3 SCAR=11.0
336 6 my $self = shift;
337  
338 11 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
339 6 ▶ croak("Cannot call $generic: Private method to $class.")
Branch: 1 branch
BranchTrueFalseTotal
unless $class->isa(_my_caller()) 3 3 6
340 unless $class->isa(Class::Simple::_my_caller());
341 3 my $ref = refaddr($self);
342 3 ▶ return (scalar(@_)
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ ? : 2 1 3
343 ? $self->$setter(@_)
344 : $self->$getter());
345 7 };
346 7 my $ugen = "_${generic}";
347 7 *$ugen = *$generic;
348 }
349 }
350  
351  
352  
353 #
354 # Bubble up the caller() stack until we leave this package.
355 #
356 sub _my_caller
357 38 ▶{
Subroutine _my_caller: called CC=3 SCAR=11.7
358 38 ▶ for (my $i = 0; my $c = caller($i); ++$i)
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = caller $i) 92 0 92
359 {
360 92 ▶ return ($c) unless $c eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
unless $c eq __PACKAGE__ 38 54 92
361 }
362 0 return (__PACKAGE__); # Shouldn't get here but just in case
363 }
364  
365  
366  
367 #
368 # This will not be called if the child classes have
369 # their own. In case they don't (and they really shouldn't
370 # because they should be using BUILD() instead), this is the default.
371 #
372 sub init
373 21 ▶{
Subroutine init: called CC=4 SCAR=13.9
Pod documented
374 21 my $self = shift;
375  
376 #
377 # If we see an even number of arguments, assume they are initializers.
378 # Don't like that behavior? Override init().
379 #
380 21 ▶ if (scalar(@_) && scalar(@_) % 2 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ and scalar(@_) % 2 == 0) 2 19 21
Condition100%3/3
scalar @_ and scalar(@_) % 2 == 0
!ll&&!rl&&r
1812
MC/DC100%2/2
scalar @_ and scalar(@_) % 2 == 0
scalar @_ scalar(@_) % 2 == 0
Truth table100%3/3
A and B
A= scalar @_B= scalar(@_) % 2 == 0
ABresult
0X0
100
111
381 2 my %args = @_;
382 2 ▶ while ( my ($k, $v) = each(%args) )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %args) 3 1 4
383 {
384 3 $self->$k($v);
385 }
386 }
387  
388 20 $self->_travel_isa('init', 'BUILD', @_);
389 20 return ($self);
390 }
391  
392  
393  
394 ##
395 ## toJson() and fromJson() are DUMP and SLURP equivalents for JSON.
396 ## I'm not sure if they're all that useful yet so they're silently
397 ## lurking here for now.
398 ##
399 #sub toJson
400 #{
401 #my $self = shift;
402 #
403 # croak("Cannot use toJson(): module JSON::XS not found.\n")
404 # unless (eval 'require JSON::XS; 1');
405 #
406 # my $ref = refaddr($self);
407 # my $json = JSON::XS->new();
408 # return $json->encode($STORAGE{$ref});
409 #}
410 #
411 #
412 #
413 #sub fromJson
414 #{
415 #my $self = shift;
416 #my $str = shift;
417 #
418 # return $self unless $str;
419 #
420 # croak("Cannot use fromJson(): module JSON::XS not found.\n")
421 # unless (eval 'require JSON::XS; 1');
422 #
423 # my $json = JSON::XS->new();
424 # my $obj = $json->decode($str);
425 # my $ref = refaddr($self);
426 # $STORAGE{$ref} = $obj;
427 #
428 # return ($self);
429 #}
430  
431  
432  
433 #
434 # Callback for Storable to serialize objects.
435 #
436 sub STORABLE_freeze
437 1 ▶{
Subroutine STORABLE_freeze: called CC=2 SCAR=7.0
Pod documented
438 1 my $self = shift;
439 1 my $cloning = shift;
440  
441 1 ▶ croak("Cannot use STORABLE_freeze(): module Storable not found.\n")
Branch: 1 branch
BranchTrueFalseTotal
unless eval "require Storable; 1" 0 1 1
442 unless (eval 'require Storable; 1');
443  
444 1 my $ref = refaddr($self);
445 1 return Storable::freeze($STORAGE{$ref});
446 }
447  
448  
449  
450 #
451 # Callback for Storable to reconstitute serialized objects.
452 #
453 sub STORABLE_thaw
454 1 ▶{
Subroutine STORABLE_thaw: called CC=2 SCAR=7.0
Pod documented
455 1 my $self = shift;
456 1 my $cloning = shift;
457 1 my $serialized = shift;
458  
459 1 ▶ croak("Cannot use STORABLE_thaw(): module Storable not found.\n")
Branch: 1 branch
BranchTrueFalseTotal
unless eval "require Storable; 1" 0 1 1
460 unless (eval 'require Storable; 1');
461  
462 1 my $ref = refaddr($self);
463 1 $STORAGE{$ref} = Storable::thaw($serialized);
464 }
465  
466 11 1;
467 __END__
↑ Summary