Perl Module Basics
This should definitely look familiar by now it's the same basic constructor pattern
you've seen earlier in the chapter.
The rest of the methods are more interesting. FETCH() is similar to the methods
in a tied scalar, but it receives an extra parameter the key that's being requested.
The implementation here is very simple:
# method called when an entry is read from the hash
sub FETCH {
my ($self, $key) = @_;
# check cache and return if found
return $self >{cache}{$key} if exists $self >{cache}{$key};
# make lookup using nameserver provided to TIEHASH
my $result = _do_dns_lookup($self >{nameserver}, $key);
# cache result and reverse mapping
$self >{cache}{$key} = $result;
$self >{cache}{$result} = $key;
# return result
return $result;
}
It's debatable whether BOA::Network::DNS should even provide a STORE()
method DNS entries are generally considered to be read only! However, for the
sake of completeness, let's provide one. STORE() takes two parameters, the key and
the value to be set for that key:
# called when an entry is written to the hash
sub STORE {
my ($self, $key, $value) = @_;
# store the value in the cache, forward and reverse
$self >{cache}{$key} = $value;
$self >{cache}{$value} = $key;
# return the value stored so that chained assignment works
return $value;
}
Perl's hashes distinguish between an entry containing undef and an entry that
doesn't exist at all. The defined() operator simply calls FETCH() on tied hashes, but
61
61
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