root/branches/release-40/t/lib/Test/Deep/Cache/Simple.pm @ 2583

Revision 2583, 1.1 kB (checked in by mpaschal, 18 months ago)

Add Test::Deep to t/lib, since we already use it in driver-tests.pl
BugzID: 79953

Line 
1use strict;
2use warnings;
3
4package Test::Deep::Cache::Simple;
5use Carp qw( confess );
6
7use Scalar::Util qw( refaddr );
8
9BEGIN
10{
11  if (grep /^weaken$/, @Scalar::Util::EXPORT_FAIL)
12  {
13    # we're running on a version of perl that has no weak refs, so we
14    # just install a no-op sub for weaken instead of importing it
15    *weaken = sub {};
16  }
17  else
18  {
19    Scalar::Util->import('weaken');
20  }
21}
22
23sub new
24{
25        my $pkg = shift;
26
27        my $self = bless {}, $pkg;
28
29        return $self;
30}
31
32sub add
33{
34        my $self = shift;
35
36        my ($d1, $d2) = @_;
37        {
38                local $SIG{__DIE__};
39
40                # cannot weaken read only refs, no harm if we can't as they never
41                # disappear
42                eval{weaken($d1)};
43                eval{weaken($d2)};
44        }
45
46        $self->{fn_get_key(@_)} = [$d1, $d2];
47}
48
49sub cmp
50{
51        my $self = shift;
52
53        my $key = fn_get_key(@_);
54        my $pair = $self->{$key};
55
56        # are both weakened refs still valid, if not delete this entry
57        if (ref($pair->[0]) and ref($pair->[1]))
58        {
59                return 1;
60        }
61        else
62        {
63                delete $self->{$key};
64                return 0;
65        }
66}
67
68sub absorb
69{
70        my $self = shift;
71
72        my $other = shift;
73
74        @{$self}{keys %$other} = values %$other;
75}
76
77sub fn_get_key
78{
79        return join(",", sort (map {refaddr($_)} @_));
80}
811;
Note: See TracBrowser for help on using the browser.