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   261 use strict;
  41         156  
  41         1694  
2 41     41   215 use warnings;
  41         130  
  41         3156  
3              
4             package Test::Deep::Cache::Simple 1.205;
5              
6 41     41   271 use Carp qw( confess );
  41         81  
  41         2809  
7              
8 41     41   272 use Scalar::Util qw( refaddr );
  41         86  
  41         6814  
9              
10             BEGIN
11             {
12 41 50   41   242 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         17815 Scalar::Util->import('weaken');
21             }
22             }
23              
24             sub new
25             {
26 2736     2736 0 4750 my $pkg = shift;
27              
28 2736         4903 my $self = bless {}, $pkg;
29              
30 2736         8098 return $self;
31             }
32              
33             sub add
34             {
35 1139     1139 0 1773 my $self = shift;
36              
37 1139         2375 my ($d1, $d2) = @_;
38             {
39 1139         1740 local $SIG{__DIE__};
  1139         4632  
40              
41 1139         2077 local $@;
42              
43             # cannot weaken read only refs, no harm if we can't as they never
44             # disappear
45 1139         2286 eval{weaken($d1)};
  1139         2205  
46 1139         1723 eval{weaken($d2)};
  1139         4279  
47             }
48              
49 1139         3479 $self->{fn_get_key(@_)} = [$d1, $d2];
50             }
51              
52             sub cmp
53             {
54 2512     2512 0 5694 my $self = shift;
55              
56 2512         5019 my $key = fn_get_key(@_);
57 2512         5655 my $pair = $self->{$key};
58              
59             # are both weakened refs still valid, if not delete this entry
60 2512 100 66     6772 if (ref($pair->[0]) and ref($pair->[1]))
61             {
62 40         193 return 1;
63             }
64             else
65             {
66 2472         4483 delete $self->{$key};
67 2472         8351 return 0;
68             }
69             }
70              
71             sub absorb
72             {
73 304     304 0 454 my $self = shift;
74              
75 304         511 my $other = shift;
76              
77 304         774 @{$self}{keys %$other} = values %$other;
  304         1157  
78             }
79              
80             sub fn_get_key
81             {
82 3651     3651 0 6689 return join(",", sort (map {refaddr($_)} @_));
  7302         24469  
83             }
84             1;
85              
86             __END__