require "stash.pl";
package STASHREFS;
use Tie::Hash;

sub TIEHASH { 
    my ($name, $dbfilename, $page) = @_;
    my $self={}; bless $self; 
    my %idb;
    $self->{"page"} = $page;
    $self->{"filename"} = $dbfilename.".inv";
    tie %idb, STASH, $self->{"filename"};
    %{$self->{"refs"}} = map { $_,$_ } split(/:/,$idb{$page});
    untie %idb;
    return $self;
}
sub FETCH {
    my $self = shift;
    my $key = shift;
    return $self->{"refs"}->{$key};
}
sub EXISTS {
    my $self = shift;
    my $key = shift;
    return exists $self->{"refs"}->{$key};
}
sub STORE {
    my $self = shift;
    my ($key, $value) = @_;
    return $self->{"refs"}->{$key} = $value;
}
sub DELETE {
    my $self = shift;
    my ($key, $value) = @_;
    delete $self->{"refs"}->{$key};
}

sub FIRSTKEY {
    my $self = shift;
    my $a = scalar keys %{$self->{"refs"}};
    return each %{$self->{"refs"}};
}
sub NEXTKEY {
    my $self = shift;
    my $lastkey = shift;
    return each %{$self->{"refs"}};
}

sub DESTROY {
    my $self = shift;
    my %idb;
    tie %idb, STASH, $self->{"filename"};
    $idb{$self->{"page"}} = join(":",keys %{$self->{"refs"}});
    untie %idb;
}

1;
