jfroebe has asked for the wisdom of the Perl Monks concerning the following question:
I'm trying to reduce the amount of name lookups I'm performing... However, I'm coding this for within a class so I'm not quite certain. I'm new to perl classes...
My code
sub syb_gethostbyaddr { my $self = shift; my $class = ref($self); my ($my_ipaddr) = @_; return (gethostbyaddr(inet_aton($my_ipaddr), AF_INET)) }
in essence, can someone explain the example to me? the second and third lines I mean.
from Programming Perl
Cache entries from files (like passwd and group files) that are apt to be reused. It's particularly important to cache entries from the network. For example, to cache the return value from gethostbyaddr when you are converting numeric addresses (like 204.148.40.9) to names (like "www.oreilly.com"), you can use something like:
I can create a local variable using something like local SybaseInterfaces::{$my_test_var}; but without understanding the simple example...
UPDATE
Here is the new code:
Man I feel like an idiot for not seeing it right off the bat... grrr... must be lack of caffeine - that's itpackage SybaseInterfaces; .... my %lookup_iptoname = (); sub syb_gethostbyaddr { my $self = shift; my $class = ref($self); my ($my_ipaddr) = @_; # We cache the name lookups to increase performance and reduce ne +twork i/o unless (defined $SybaseInterfaces::lookup_iptoname{$my_ipaddr}) { $SybaseInterfaces::lookup_iptoname{$my_ipaddr} = gethostbyaddr +(inet_aton($my_ipaddr), AF_INET); } return ( $SybaseInterfaces::lookup_iptoname{$my_ipaddr} ); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: local variables and classes
by Fletch (Bishop) on Mar 11, 2004 at 19:47 UTC | |
|
Re: local variables and classes
by Ovid (Cardinal) on Mar 11, 2004 at 20:04 UTC | |
|
Re: local variables and classes
by TomDLux (Vicar) on Mar 11, 2004 at 20:31 UTC | |
by jfroebe (Parson) on Mar 11, 2004 at 20:59 UTC | |
by jfroebe (Parson) on Mar 11, 2004 at 21:20 UTC |