Integrate Memoize 0.64. Few tweaks were required in
[p5sagit/p5-mst-13.2.git] / lib / Memoize / SDBM_File.pm
1 package Memoize::SDBM_File;
2 use SDBM_File;
3 @ISA = qw(SDBM_File);
4
5 $Verbose = 0;
6
7 sub AUTOLOAD {
8   warn "Nonexistent function $AUTOLOAD invoked in Memoize::SDBM_File\n";
9 }
10
11 sub import {
12   warn "Importing Memoize::SDBM_File\n" if $Verbose;
13 }
14
15
16 my %keylist;
17
18 # This is so ridiculous...
19 sub _backhash {
20   my $self = shift;
21   my %fakehash;
22   my $k; 
23   for ($k = $self->FIRSTKEY(); defined $k; $k = $self->NEXTKEY($k)) {
24     $fakehash{$k} = undef;
25   }
26   $keylist{$self} = \%fakehash;
27 }
28
29 sub EXISTS {
30   warn "Memoize::SDBM_File EXISTS (@_)\n" if $Verbose;
31   my $self = shift;
32   _backhash($self)  unless exists $keylist{$self};
33   my $r = exists $keylist{$self}{$_[0]};
34   warn "Memoize::SDBM_File EXISTS (@_) ==> $r\n" if $Verbose;
35   $r;
36 }
37
38 sub DEFINED {
39   warn "Memoize::SDBM_File DEFINED (@_)\n" if $Verbose;
40   my $self = shift;
41   _backhash($self)  unless exists $keylist{$self};
42   defined $keylist{$self}{$_[0]};
43 }
44
45 sub DESTROY {
46   warn "Memoize::SDBM_File DESTROY (@_)\n" if $Verbose;
47   my $self = shift;
48   delete $keylist{$self};   # So much for reference counting...
49   $self->SUPER::DESTROY(@_);
50 }
51
52 # Maybe establish the keylist at TIEHASH time instead?
53
54 sub STORE {
55   warn "Memoize::SDBM_File STORE (@_)\n" if $VERBOSE;
56   my $self = shift;
57   $keylist{$self}{$_[0]} = undef;
58   $self->SUPER::STORE(@_);
59 }
60
61 # Inherit FETCH and TIEHASH
62
63 1;