Chapter 2
exists() needs special support from the tied implementation in the form of
EXISTS(). To complete the picture, DELETE() must be provided to remove a key from
the hash, after which it is expected that EXISTS() will return false for that key. It is
often difficult to decide what behavior to provide for these calls on a tied hash. In
this case, you'd want to do the simple thing and just examine the underlying cache:
# method called when exists() is called on the hash
sub EXISTS {
my ($self, $key) = @_;
return exists $self >{cache}{$key};
}
# method called when delete() is called on the hash
sub DELETE {
my ($self, $key) = @_;
# delete both forward and reverse lookups if the key exists
my $value;
if (exists $self >{cache}{$key}) {
$value = $self >{cache}{$key};
delete $self >{cache}{$value};
delete $self >{cache}{$key};
}
# return deleted value, just like the normal delete()
return $value;
}
Perl provides a hook for a special case of delete() when the entire hash is
being cleared. This is triggered by assigning an empty list to a hash:
%dns = ();
It's possible to implement this by looping over the keys and calling DELETE(), but
there's usually a more efficient implementation. In this case you can just clear the
cache:
sub CLEAR {
my $self = shift;
%{$self >{cache}} = ();
}
62
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved