Perl Module Basics
Finally, you must provide an iterator by implementing FIRSTKEY() and NEXTKEY().
The iterator functions are used when several Perl operators are called keys(),
values(), and each(). The utility of allowing users to iterate over DNS lookups in
the cache is questionable, but here's a possible implementation:
sub FIRSTKEY {
my $self = shift;
# reset iterator for the cache
scalar keys %{$self >{cache}};
# return the first key from the cache
return scalar each %{$self >{cache}};
}
sub NEXTKEY {
my ($self, $lastkey) = @_;
return scalar each %{$self >{cache}};
}
This implementation just proxies the call to each() on the underlying cache. As a
result, it doesn't use the second parameter to NEXTKEY() the last key returned. This
can be useful if the underlying data store isn't a hash but rather something that
maintains an order.
Other Ties
In addition to scalars and hashes, you can also tie arrays and file handles. Once
you've grokked implementing tied scalars and hashes, it's just a matter of learning
the specific method names and interfaces. You can get this information from Perl's
documentation with the command perlpod perltie.
Tying and Overloading
You might imagine that you could combine tying and overloading to form the
ultimate magic Perl module. Unfortunately, due to a bug in the implementation of
overloading in the version of Perl I'm using (5.6.1) and older versions, this isn't
easily done. You can find the rather complicated details in the overload documen
tation, but suffice it to say that for the time being you should choose tying or
overloading, not both.
63
63
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