blib/lib/Class/InsideOut.pm

filter: Statementstmt 94.9% 299 / 315 Branchbran 87.5% 98 / 112 Conditioncond 90.3% 28 / 31 MC/DCmcdc 75.0% 12 / 16 Subroutinesub 96.0% 49 / 51 Podpod 100% 7 / 7 totaltotal 92.6% 493 / 532 CC 88 SCAR 17.1
CC
88
Cov
93%
CLONE
20.0
new
13.0
_revert
9.2
CRAP 5.5
↑ Summary
1 package Class::InsideOut;
2 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 # ABSTRACT: a safe, simple inside-out object construction kit
4  
5 24 our $VERSION = '1.14';
6  
7 24 ▶use vars qw/@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 24 @ISA = qw ( Exporter );
10 24 @EXPORT = qw ( ); # nothing by default
11 24 @EXPORT_OK = qw ( new id options private property public readonly register );
12 24 %EXPORT_TAGS = (
13 "std" => [ qw( id private public readonly register ) ],
14 "new" => [ qw( new ) ],
15 "all" => [ @EXPORT_OK ],
16 "singleton" => [], # just a flag for import()
17 );
18  
19 24 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 24 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 24 ▶use Class::ISA;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 24 ▶use Scalar::Util 1.09 qw( refaddr reftype blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 # Check for XS Scalar::Util with weaken() or warn and fallback
25 # syntax of error changed in Scalar::Util so we check both versions
26 24 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=9.2
27 24 eval { Scalar::Util->import( "weaken" ) };
28 24 ▶ if ( $@ =~ /\AWeak references|weaken is only available/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /\AWeak references|weaken is only available/) 0 24 24
29 0 warn "Scalar::Util::weaken unavailable: "
30 . "Class::InsideOut will not be thread-safe and will leak memory\n";
31 0 ▶ *weaken = sub { return @_ };
Subroutine __ANON__: not called CC=1 SCAR=6.9
32 }
33 }
34  
35 #--------------------------------------------------------------------------#
36 # Class data
37 #--------------------------------------------------------------------------#
38  
39 24 my %PROP_DATA_FOR; # class => { prop_name => property hashrefs }
40 my %PUBLIC_PROPS_FOR; # class => { prop_name => 1 }
41 24 my %CLASS_ISA; # class => [ list of self and @ISA tree ]
42 24 my %OPTIONS; # class => { default accessor options }
43 24 my %OBJECT_REGISTRY; # refaddr => weak object reference
44  
45 #--------------------------------------------------------------------------#
46 # option validation parameters
47 #--------------------------------------------------------------------------#
48  
49 # Private but global so related classes can define their own valid options
50 # if they need them. Modify at your own risk. Done this way so as to
51 # avoid creating class functions to do the same basic thing
52  
53 24 ▶use vars qw( %_OPTION_VALIDATION );
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 36 ▶sub __coderef {
Subroutine __coderef: called CC=5 SCAR=16.7
56 36 ▶ return 1 if reftype($_[0])||"" eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if 34 2 36
Condition100%2/2
builtin::reftype($_[0]) or
l!l
342
MC/DC100%1/1
builtin::reftype($_[0]) or
builtin::reftype($_[0])
Truth table100%2/2
A
A= builtin::reftype($_[0])
Aresult
11
00
57
58 # Avoid loading overload.pm unless we'd have to die otherwise
59 2 require overload;
60 2 ▶ return 1 if overload::Overloaded($_[0]) && overload::Method($_[0], q[&{}]);
Branch: 1 branch
BranchTrueFalseTotal
if overload::Overloaded($_[0]) and overload::Method($_[0], "&{}") 0 2 2
Condition33%1/3
overload::Overloaded($_[0]) and overload::Method($_[0], "&{}")
!ll&&!rl&&r
200
MC/DC0%0/2
overload::Overloaded($_[0]) and overload::Method($_[0], "&{}")
overload::Overloaded($_[0]) overload::Method($_[0], "&{}")
Truth table33%1/3
A and B
A= overload::Overloaded($_[0])B= overload::Method($_[0], "&{}")
ABresult
0X0
100
111
61
62 2 die "must be a code reference";
63 }
64  
65 %_OPTION_VALIDATION = (
66 29 ▶ privacy => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
67 29 my $v = shift;
68 29 ▶ $v =~ /public|private/ or die "'$v' is not a valid privacy setting"
Branch: 1 branch
BranchTrueFalseTotal
$v =~ /public|private/ or die "'${v}' is not a valid privacy setting" 28 1 29
69 },
70 24 set_hook => \&__coderef,
71 get_hook => \&__coderef,
72 );
73  
74 #--------------------------------------------------------------------------#
75 # public functions
76 #--------------------------------------------------------------------------#
77  
78 42 ▶sub import {
Subroutine import: called CC=2 SCAR=6.9
79 24 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
80 42 my $caller = caller;
81 42 *{ "$caller\::DESTROY" } = _gen_DESTROY( $caller );
82 # check for ":singleton" and do export attach instead of thaw
83 42 ▶ if ( grep { $_ eq ":singleton" } @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq ':singleton';} @_) { } 3 39 42
84 3 *{ "$caller\::STORABLE_freeze" } = _gen_STORABLE_freeze( $caller, 1 );
85 3 *{ "$caller\::STORABLE_attach" } = _gen_STORABLE_attach( $caller );
86 3 @_ = grep { $_ ne ':singleton' } @_; # strip it back out
87 }
88 else {
89 39 *{ "$caller\::STORABLE_freeze" } = _gen_STORABLE_freeze( $caller, 0 );
90 39 *{ "$caller\::STORABLE_thaw" } = _gen_STORABLE_thaw( $caller );
91 }
92 42 local $Exporter::ExportLevel = $Exporter::ExportLevel + 1;
93 42 &Exporter::import;
94 }
95  
96 24 ▶BEGIN { *id = \&Scalar::Util::refaddr; }
Subroutine BEGIN: called CC=1 SCAR=0.0
97  
98 24 ▶sub options {
Subroutine options: called CC=2 SCAR=6.9
Pod documented
99 24 my $opt = shift;
100 24 my $caller = caller;
101 24 ▶ _check_options( $opt ) if defined $opt;
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt 22 2 24
102 18 return %{ $OPTIONS{ $caller } = _merge_options( $caller, $opt ) };
103 }
104
105 7 ▶sub new {
Subroutine new: called CC=13 SCAR=25.7
Pod documented
106 7 my $class = shift;
107 7 ▶ croak "new() must be called as a class method"
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 7 7
108 if ref $class;
109 7 my $self = register( $class );
110 7 ▶ return $self unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 6 7
111
112 # initialization
113 6 ▶ croak "Arguments to new must be a hash or hash reference"
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 and not ref $_[0] && builtin::reftype($_[0]) eq "HASH" or @_ > 1 and @_ % 2 3 3 6
Condition100%12/12
@_ == 1 and not ref $_[0] && builtin::reftype($_[0]) eq "HASH" or @_ > 1 and @_ % 2
l!l&&r!l&&!r
213
@_ == 1 and not ref $_[0] && builtin::reftype($_[0]) eq "HASH"
!ll&&!rl&&r
312
ref $_[0] && builtin::reftype($_[0]) eq 'HASH'
!ll&&!rl&&r
111
@_ > 1 and @_ % 2
!ll&&!rl&&r
121
MC/DC80%4/5
@_ == 1 and not ref $_[0] && builtin::reftype($_[0]) eq "HASH" or @_ > 1 and @_ % 2
@_ == 1 ref $_[0] builtin::reftype($_[0]) eq 'HASH' @_ > 1 @_ % 2
Truth table62%5/8
A and not(B && C) or D and E
A= @_ == 1B= ref $_[0]C= builtin::reftype($_[0]) eq 'HASH'D= @_ > 1E= @_ % 2
ABCDEresult
10XXX1
110XX1
0XX111
111111
0XX0X0
0XX100
1110X0
111100
114 if ( @_ == 1 && ! ( ref $_[0] && reftype($_[0]) eq 'HASH' ) )
115 || ( @_ > 1 && @_ % 2 );
116
117 3 ▶ my %args = (@_ == 1) ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 1 2 3
118  
119 3 for my $prop ( keys %args ) {
120 5 for my $c ( _class_tree( $class ) ) {
121 9 my $properties = $PROP_DATA_FOR{ $c };
122 9 ▶ next unless $properties;
Branch: 1 branch
BranchTrueFalseTotal
unless $properties 0 9 9
123 9 ▶ if ( exists $properties->{$prop} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $properties->{$prop}) 7 2 9
124 7 $properties->{$prop}{ refaddr $self } = $args{$prop};
125 }
126 }
127 }
128  
129 3 return $self;
130 }
131  
132 15 ▶sub private($\%;$) { ## no critic -- prototype
Subroutine private: called CC=2 SCAR=6.9
Pod documented
133 15 &_check_property;
134 10 ▶ $_[2] ||= {};
Condition100%2/2
$_[2] ||= {}
l!l
37
MC/DC100%1/1
$_[2] ||= {}
$_[2]
Truth table100%2/2
A
A= $_[2]
Aresult
11
00
135 10 $_[2] = { %{$_[2]}, privacy => 'private' };
136 10 goto &_install_property;
137 }
138  
139 49 ▶sub property($\%;$) { ## no critic -- prototype
Subroutine property: called CC=1 SCAR=0.0
Pod documented
140 49 &_check_property;
141 43 goto &_install_property;
142 }
143  
144 50 ▶sub public($\%;$) { ## no critic -- prototype
Subroutine public: called CC=2 SCAR=6.9
Pod documented
145 50 &_check_property;
146 45 ▶ $_[2] ||= {};
Condition100%2/2
$_[2] ||= {}
l!l
2619
MC/DC100%1/1
$_[2] ||= {}
$_[2]
Truth table100%2/2
A
A= $_[2]
Aresult
11
00
147 45 $_[2] = { %{$_[2]}, privacy => 'public' };
148 45 goto &_install_property;
149 }
150  
151 2 ▶sub readonly($\%;$) { ## no critic -- prototype
Subroutine readonly: called CC=2 SCAR=7.0
Pod documented
152 2 &_check_property;
153 2 ▶ $_[2] ||= {};
Condition50%1/2
$_[2] ||= {}
l!l
02
MC/DC0%0/1
$_[2] ||= {}
$_[2]
Truth table50%1/2
A
A= $_[2]
Aresult
11
00
154 $_[2] = {
155 2 %{$_[2]},
156 privacy => 'public',
157 1 ▶ set_hook => sub { die "is read-only\n" }
Subroutine __ANON__: called CC=1 SCAR=0.0
158 2 };
159 2 goto &_install_property;
160 }
161  
162 59 ▶sub register {
Subroutine register: called CC=5 SCAR=16.1
Pod documented
163 59 my ($obj);
164 59 ▶ if ( @_ == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 0) { } 1 58 59
elsif (@_ == 1) { } 56 2 58
165 # register()
166 1 croak "Invalid call to register(): empty argument list"
167 }
168 elsif ( @_ == 1 ) {
169 # register( OBJECT | CLASSNAME )
170 56 ▶ if ( blessed $_[0] ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($_[0])) { } 46 10 56
elsif (ref \$_[0] eq 'SCALAR') { } 9 1 10
171 46 $obj = shift;
172 }
173 elsif ( ref \$_[0] eq 'SCALAR' ) {
174 9 $obj = \(my $scalar);
175 9 bless $obj, shift;
176 }
177 else {
178 1 croak "Invalid argument '$_[0]' to register(): " .
179 "must be an object or class name"
180 }
181 }
182 else {
183 # register( REFERENCE/OBJECT, CLASSNAME )
184 2 $obj = shift;
185 2 bless $obj, shift; # ok to rebless
186 }
187
188 57 weaken( $OBJECT_REGISTRY{ refaddr $obj } = $obj );
189 57 return $obj;
190 }
191  
192 #--------------------------------------------------------------------------#
193 # private functions for implementation
194 #--------------------------------------------------------------------------#
195  
196 # Registering is global to avoid having to register objects for each class.
197 # CLONE is not exported but CLONE in Class::InsideOut updates all registered
198 # objects for all properties across all classes
199  
200 0 ▶sub CLONE {
Subroutine CLONE: not called CC=4 SCAR=30.0
201 0 my $class = shift;
202  
203 # assemble references to all properties for all classes
204 0 my @properties = map { values %$_ } values %PROP_DATA_FOR;
205  
206 0 for my $old_id ( keys %OBJECT_REGISTRY ) {
207  
208 # retrieve the new object and id
209 0 my $object = $OBJECT_REGISTRY{ $old_id };
210 0 my $new_id = refaddr $object;
211  
212 # for all properties, relocate data to the new id if
213 # the property has data under the old id
214 0 for my $prop ( @properties ) {
215 0 ▶ next unless exists $prop->{ $old_id };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $prop->{$old_id} 0 0 0
216 0 $prop->{ $new_id } = $prop->{ $old_id };
217 0 delete $prop->{ $old_id };
218 }
219  
220 # update the registry to the new, cloned object
221 0 weaken ( $OBJECT_REGISTRY{ $new_id } = $object );
222 0 _deregister( $old_id );
223 }
224 }
225  
226 66 ▶sub _check_options{
Subroutine _check_options: called CC=5 SCAR=16.1
227 66 my ($opt) = @_;
228 66 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
229  
230 66 ▶ croak "Invalid options argument '$opt': must be a hash reference"
Branch: 1 branch
BranchTrueFalseTotal
if ref $opt ne "HASH" 8 58 66
231 if ref $opt ne 'HASH';
232  
233 58 my @valid_keys = keys %_OPTION_VALIDATION;
234 58 for my $key ( keys %$opt ) {
235 croak "Invalid option '$key': unknown option"
236 66 ▶ if ! grep { $_ eq $key } @valid_keys;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $key;} @valid_keys 1 65 66
237 65 eval { $_OPTION_VALIDATION{$key}->( $opt->{$key} ) };
238 65 ▶ croak "Invalid option '$key': $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 3 62 65
239 }
240
241 54 return;
242 }
243  
244 116 ▶sub _check_property {
Subroutine _check_property: called CC=4 SCAR=13.9
245 116 my ($label, $hash, $opt) = @_;
246 116 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
247 116 ▶ croak "Invalid property name '$label': must be a perl identifier"
Branch: 1 branch
BranchTrueFalseTotal
unless $label =~ /\A[a-z_]\w*\z/i 9 107 116
248 if $label !~ /\A[a-z_]\w*\z/i;
249 croak "Duplicate property name '$label'"
250 107 ▶ if grep { $_ eq $label } keys %{ $PROP_DATA_FOR{ caller(1) } };
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $label;} keys %{$PROP_DATA_FOR{caller 1};} 1 106 107
251 106 ▶ _check_options( $opt ) if defined $opt;
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt 44 62 106
252 100 return;
253 }
254  
255 130 ▶sub _class_tree {
Subroutine _class_tree: called CC=2 SCAR=6.9
256 130 my $class = shift;
257 130 ▶ $CLASS_ISA{ $class } ||= [ Class::ISA::self_and_super_path( $class ) ];
Condition100%2/2
$CLASS_ISA{$class} ||= [Class::ISA::self_and_super_path($class)]
l!l
10327
MC/DC100%1/1
$CLASS_ISA{$class} ||= [Class::ISA::self_and_super_path($class)]
$CLASS_ISA{$class}
Truth table100%2/2
A
A= $CLASS_ISA{$class}
Aresult
11
00
258 130 return @{ $CLASS_ISA{ $class } };
259 }
260  
261 # take either object or object id
262 48 ▶sub _deregister {
Subroutine _deregister: called CC=2 SCAR=7.0
263 48 my ($arg) = @_;
264 48 ▶ my $obj_id = ref $arg ? refaddr $arg : $arg;
Branch: 1 branch
BranchTrueFalseTotal
ref $arg ? : 48 0 48
265 48 delete $OBJECT_REGISTRY{ $obj_id };
266 48 return;
267 }
268  
269 # turn object into hash -- see _revert()
270 19 ▶sub _evert {
Subroutine _evert: called CC=8 SCAR=20.9
271 19 my ( $obj ) = @_;
272
273 # Extract properties to save
274 19 my %property_vals;
275 19 for my $c ( _class_tree( ref $obj) ) {
276 25 ▶ next unless exists $PROP_DATA_FOR{ $c };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $PROP_DATA_FOR{$c} 0 25 25
277 25 my $properties = $PROP_DATA_FOR{ $c };
278 25 for my $prop ( keys %$properties ) {
279 my $value = exists $properties->{$prop}{ refaddr $obj }
280 53 ▶ ? $properties->{$prop}{ refaddr $obj }
Branch: 1 branch
BranchTrueFalseTotal
exists $properties->{$prop}{builtin::refaddr($obj)} ? : 35 18 53
281 : undef ;
282 53 $property_vals{$c}{$prop} = $value;
283 }
284 }
285  
286 # extract object reference contents (by type)
287 19 my $type = reftype $obj;
288 19 ▶ my $contents = $type eq 'SCALAR' ? \do{ my $s = $$obj }
Branch: 3 branches
BranchTrueFalseTotal
$type eq 'SCALAR' ? : 6 13 19
$type eq 'ARRAY' ? : 12 1 13
$type eq 'HASH' ? : 1 0 1
289 : $type eq 'ARRAY' ? [ @$obj ]
290 : $type eq 'HASH' ? { %$obj }
291 : undef # other types not supported
292 ;
293  
294 # assemble reference to hand back
295 return {
296 19 class => ref $obj,
297 type => $type,
298 contents => $contents,
299 properties => \%property_vals
300 };
301 }
302  
303 55 ▶sub _gen_accessor {
Subroutine _gen_accessor: called CC=1 SCAR=0.0
304 55 my ($ref) = @_;
305 82 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
306 82 my $obj = shift;
307 82 my $obj_id = refaddr $obj;
308 82 ▶ $ref->{ $obj_id } = shift if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 31 51 82
309 82 return $ref->{ $obj_id };
310 55 };
311 }
312
313 28 ▶sub _gen_hook_accessor {
Subroutine _gen_hook_accessor: called CC=1 SCAR=0.0
314 28 my ($ref, $name, $get_hook, $set_hook) = @_;
315 71 ▶ return sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
316 71 my ($obj,@args) = @_;
317 71 my $obj_id = refaddr $obj;
318 71 ▶ if (@args) {
Branch: 2 branches
BranchTrueFalseTotal
if (@args) { } 37 34 71
elsif ($get_hook) { } 20 14 34
319 37 local *_ = \($args[0]);
320 37 ▶ if ($set_hook) {
Branch: 1 branch
BranchTrueFalseTotal
if ($set_hook) { } 33 4 37
321 33 eval { $set_hook->(@args) };
322 33 ▶ if ( $@ ) { chomp $@; croak "$name() $@" }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 9 24 33
323 24 $ref->{ $obj_id } = shift @args;
324 }
325 else {
326 4 $ref->{ $obj_id } = shift @args;
327 }
328 }
329 elsif ($get_hook) {
330 20 local $_ = $ref->{ $obj_id };
331 20 my ( $value, @value );
332 20 ▶ if ( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 12 8 20
333 12 @value = eval { $get_hook->() };
334 }
335 else {
336 8 $value = eval { $get_hook->() };
337 }
338 20 ▶ if ( $@ ) { chomp $@; croak "$name() $@" }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 4 16 20
339 16 ▶ return wantarray ? @value : $value;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 12 4 16
340 }
341 else {
342 14 return $ref->{ $obj_id };
343 }
344 28 };
345 }
346
347 42 ▶sub _gen_DESTROY {
Subroutine _gen_DESTROY: called CC=1 SCAR=0.0
348 42 my $class = shift;
349 48 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
350 48 my $obj = shift;
351 48 my $obj_id = refaddr $obj; # cache for later property deletes
352  
353 # Call a custom DEMOLISH hook if one exists.
354 48 my $demolish;
355 {
356 24 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
357 48 $demolish = *{ "$class\::DEMOLISH" }{CODE};
358 }
359 48 ▶ $demolish->($obj) if defined $demolish;
Branch: 1 branch
BranchTrueFalseTotal
if defined $demolish 3 45 48
360  
361 # Clean up properties in all Class::InsideOut parents
362 48 for my $c ( _class_tree( $class ) ) {
363 79 ▶ next unless exists $PROP_DATA_FOR{ $c };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $PROP_DATA_FOR{$c} 10 69 79
364 69 delete $_->{ $obj_id } for values %{ $PROP_DATA_FOR{ $c } };
365 }
366  
367 # XXX this global registry could be deleted repeatedly
368 # in superclasses -- SUPER::DESTROY shouldn't be called by DEMOLISH
369 # it should only call SUPER::DEMOLISH if need be; still,
370 # rest of the destructor doesn't need the registry, so early deletion
371 # by a subclass should be safe
372 48 _deregister( $obj );
373  
374 48 return;
375 42 };
376 }
377  
378 3 ▶sub _gen_STORABLE_attach {
Subroutine _gen_STORABLE_attach: called CC=1 SCAR=0.0
379 3 my $class = shift;
380 5 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
381 5 my ( $class, $cloning, $serialized ) = @_;
382 5 require Storable;
383 5 my $data = Storable::thaw( $serialized );
384
385 # find a user attach hook
386 5 my $hook;
387 {
388 24 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
389 5 $hook = *{ "$class\::ATTACH" }{CODE};
390 }
391  
392 # try user hook to recreate, otherwise new(), otherwise give up
393 5 ▶ if ( defined $hook ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $hook) { } 2 3 5
elsif ($class->can('new')) { } 2 1 3
394 2 return $hook->($class, $cloning, $data);
395 }
396 elsif ( $class->can( "new" ) ) {
397 2 return $class->new();
398 }
399 else {
400 1 warn "Error attaching to $class:\n" .
401 "Couldn't find STORABLE_attach_hook() or new() in $class\n";
402 1 return;
403 }
404 3 };
405 }
406
407 42 ▶sub _gen_STORABLE_freeze {
Subroutine _gen_STORABLE_freeze: called CC=1 SCAR=0.0
408 42 my ($class, $singleton) = @_;
409 19 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
410 19 my ( $obj, $cloning ) = @_;
411  
412 # Call STORABLE_freeze_hooks in each class if they exists
413 19 for my $c ( _class_tree( ref $obj ) ) {
414 25 my $hook;
415 {
416 24 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
417 25 $hook = *{ "$c\::FREEZE" }{CODE};
418 }
419 25 ▶ $hook->($obj) if defined $hook;
Branch: 1 branch
BranchTrueFalseTotal
if defined $hook 4 21 25
420 }
421  
422 # Extract properties to save
423 19 my $data = _evert( $obj );
424  
425 19 ▶ if ( $singleton ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($singleton) { } 3 16 19
426 # can't return refs, so freeze data as string and return
427 3 require Storable;
428 3 return Storable::freeze( $data );
429 }
430 else {
431 # return $serialized, @refs
432 # serialized string doesn't matter -- all data has been moved into
433 # the additional ref
434 16 return 'BOGUS', $data;
435 }
436 42 };
437 }
438  
439 39 ▶sub _gen_STORABLE_thaw {
Subroutine _gen_STORABLE_thaw: called CC=1 SCAR=0.0
440 39 my $class = shift;
441 16 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
442 16 my ( $obj, $cloning, $serialized, $data ) = @_;
443  
444 16 _revert( $data, $obj );
445  
446 # Call STORABLE_thaw_hooks in each class if they exists
447 16 for my $c ( _class_tree( ref $obj ) ) {
448 22 my $hook;
449 {
450 24 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
451 22 $hook = *{ "$c\::THAW" }{CODE};
452 }
453 22 ▶ $hook->($obj) if defined $hook;
Branch: 1 branch
BranchTrueFalseTotal
if defined $hook 4 18 22
454 }
455  
456 16 return;
457 39 };
458 }
459  
460 100 ▶sub _install_property{
Subroutine _install_property: called CC=5 SCAR=16.1
461 100 my ($label, $hash, $opt) = @_;
462  
463 100 my $caller = caller(0); # we get here via "goto", so caller(0) is right
464 100 $PROP_DATA_FOR{ $caller }{$label} = $hash;
465 100 my $options = _merge_options( $caller, $opt );
466 100 ▶ if ( exists $options->{privacy} && $options->{privacy} eq 'public' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{"privacy"} and $options->{"privacy"} eq "public") 83 17 100
Condition100%3/3
exists $options->{"privacy"} and $options->{"privacy"} eq "public"
!ll&&!rl&&r
11683
MC/DC100%2/2
exists $options->{"privacy"} and $options->{"privacy"} eq "public"
exists $options->{"privacy"} $options->{"privacy"} eq "public"
Truth table100%3/3
A and B
A= exists $options->{"privacy"}B= $options->{"privacy"} eq "public"
ABresult
0X0
100
111
467 24 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
468 83 *{ "$caller\::$label" } =
469 ($options->{set_hook} || $options->{get_hook})
470 ? _gen_hook_accessor( $hash, $label, $options->{get_hook},
471 $options->{set_hook} )
472 83 ▶ : _gen_accessor( $hash ) ;
Branch: 1 branch
BranchTrueFalseTotal
$options->{'set_hook'} || $options->{'get_hook'} ? : 28 55 83
Condition100%3/3
$options->{"set_hook"} or $options->{"get_hook"}
l!l&&r!l&&!r
24455
MC/DC100%2/2
$options->{"set_hook"} or $options->{"get_hook"}
$options->{"set_hook"} $options->{"get_hook"}
Truth table100%3/3
A or B
A= $options->{"set_hook"}B= $options->{"get_hook"}
ABresult
1X1
011
000
473 83 $PUBLIC_PROPS_FOR{ $caller }{ $label } = 1;
474 }
475 100 return;
476 }
477  
478 118 ▶sub _merge_options {
Subroutine _merge_options: called CC=3 SCAR=11.0
479 118 my ($class, $new_options) = @_;
480 118 my @merged;
481 118 ▶ push @merged, %{ $OPTIONS{ $class } } if defined $OPTIONS{ $class };
Branch: 1 branch
BranchTrueFalseTotal
if defined $OPTIONS{$class} 53 65 118
482 118 ▶ push @merged, %$new_options if defined $new_options;
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_options 82 36 118
483 118 return { @merged };
484 }
485
486 16 ▶sub _revert {
Subroutine _revert: called CC=9 SCAR=22.2
487 16 my ( $data, $obj ) = @_;
488  
489 16 my $contents = $data->{contents};
490 16 ▶ if ( defined $obj ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $obj) { } 16 0 16
491 # restore contents to the pregenerated object
492 16 for ( reftype $obj ) {
493 3 ▶ /SCALAR/ ? do { $$obj = $$contents } :
Branch: 1 branch
BranchTrueFalseTotal
/ARRAY/ ? : 12 1 13
494 12 ▶ /ARRAY/ ? do { @$obj = @$contents } :
Branch: 1 branch
BranchTrueFalseTotal
/HASH/ ? : 1 0 1
495 1 /HASH/ ? do { %$obj = %$contents } :
496 16 ▶ do {} ;
Branch: 1 branch
BranchTrueFalseTotal
/SCALAR/ ? : 3 13 16
497 }
498 }
499 else {
500 # just use the contents as the reference
501 # and bless it back into an object
502 0 $obj = $contents;
503 }
504  
505 16 bless $obj, $data->{class};
506  
507 # restore properties
508 16 for my $c ( _class_tree( ref $obj ) ) {
509 22 my $properties = $PROP_DATA_FOR{ $c };
510 22 ▶ next unless $properties;
Branch: 1 branch
BranchTrueFalseTotal
unless $properties 0 22 22
511 22 for my $prop ( keys %$properties ) {
512 50 my $value = $data->{properties}{ $c }{ $prop };
513 50 $properties->{$prop}{ refaddr $obj } = $value;
514 }
515 }
516  
517 # register object
518 16 register( $obj );
519 16 return $obj;
520 }
521  
522 #--------------------------------------------------------------------------#
523 # private functions for use in testing
524 #--------------------------------------------------------------------------#
525  
526 12 ▶sub _object_count {
Subroutine _object_count: called CC=1 SCAR=0.0
527 12 return scalar( keys %OBJECT_REGISTRY );
528 }
529  
530 7 ▶sub _properties {
Subroutine _properties: called CC=5 SCAR=16.1
531 7 my $class = shift;
532 7 my %properties;
533 7 for my $c ( _class_tree( $class ) ) {
534 10 ▶ next if not exists $PROP_DATA_FOR{ $c };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $PROP_DATA_FOR{$c} 0 10 10
535 10 for my $p ( keys %{ $PROP_DATA_FOR{ $c } } ) {
536 41 ▶ $properties{$c}{$p} = exists $PUBLIC_PROPS_FOR{$c}{$p}
Branch: 1 branch
BranchTrueFalseTotal
exists $PUBLIC_PROPS_FOR{$c}{$p} ? : 37 4 41
537 ? "public" : "private";
538 }
539 }
540 7 return \%properties;
541 }
542  
543 8 ▶sub _leaking_memory {
Subroutine _leaking_memory: called CC=5 SCAR=16.2
544 8 my %leaks;
545  
546 8 for my $class ( keys %PROP_DATA_FOR ) {
547 21 for my $prop ( values %{ $PROP_DATA_FOR{ $class } } ) {
548 47 for my $obj_id ( keys %$prop ) {
549 $leaks{ $class }++
550 30 ▶ if not exists $OBJECT_REGISTRY{ $obj_id };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $OBJECT_REGISTRY{$obj_id} 0 30 30
551 }
552 }
553 }
554  
555 8 return keys %leaks;
556 }
557  
558 24 1; # modules must return true
559  
560 __END__
↑ Summary