File Coverage

blib/lib/Test/Deep/Cache/Simple.pm
Criterion Covered Total %
statement 40 41 97.5
branch 3 4 75.0
condition 2 3 66.6
subroutine 10 10 100.0
pod 0 5 0.0
total 55 63 87.3


line stmt bran cond sub pod time code
1 41     41   308 use strict;
  41         78  
  41         1161  
2 41     41   202 use warnings;
  41         73  
  41         1880  
3              
4             package Test::Deep::Cache::Simple 1.204;
5              
6 41     41   225 use Carp qw( confess );
  41         94  
  41         2206  
7              
8 41     41   251 use Scalar::Util qw( refaddr );
  41         109  
  41         5212  
9              
10             BEGIN
11             {
12 41 50   41   321 if (grep /^weaken$/, @Scalar::Util::EXPORT_FAIL)
13             {
14             # we're running on a version of perl that has no weak refs, so we
15             # just install a no-op sub for weaken instead of importing it
16 0         0 *weaken = sub {};
17             }
18             else
19             {
20 41         15173 Scalar::Util->import('weaken');
21             }
22             }
23              
24             sub new
25             {
26 2750     2750 0 3853 my $pkg = shift;
27              
28 2750         4716 my $self = bless {}, $pkg;
29              
30 2750         6503 return $self;
31             }
32              
33             sub add
34             {
35 1140     1140 0 1733 my $self = shift;
36              
37 1140         2011 my ($d1, $d2) = @_;
38             {
39 1140         1540 local $SIG{__DIE__};
  1140         3407  
40              
41 1140         1713 local $@;
42              
43             # cannot weaken read only refs, no harm if we can't as they never
44             # disappear
45 1140         1983 eval{weaken($d1)};
  1140         2442  
46 1140         2026 eval{weaken($d2)};
  1140         4463  
47             }
48              
49 1140         3082 $self->{fn_get_key(@_)} = [$d1, $d2];
50             }
51              
52             sub cmp
53             {
54 2515     2515 0 3570 my $self = shift;
55              
56 2515         4155 my $key = fn_get_key(@_);
57 2515         5170 my $pair = $self->{$key};
58              
59             # are both weakened refs still valid, if not delete this entry
60 2515 100 66     6083 if (ref($pair->[0]) and ref($pair->[1]))
61             {
62 40         194 return 1;
63             }
64             else
65             {
66 2475         3581 delete $self->{$key};
67 2475         7100 return 0;
68             }
69             }
70              
71             sub absorb
72             {
73 304     304 0 452 my $self = shift;
74              
75 304         403 my $other = shift;
76              
77 304         620 @{$self}{keys %$other} = values %$other;
  304         892  
78             }
79              
80             sub fn_get_key
81             {
82 3655     3655 0 6179 return join(",", sort (map {refaddr($_)} @_));
  7310         21748  
83             }
84             1;
85              
86             __END__