Hi,

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 the Programming Perl, it is a little confusing how to implement. "sub numtoname" is the subroutine and $numtoname{} is an unrelated global hash? I don't understand the desire for "local ($_) = @_;"

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:

sub numtoname { local ($_) = @_; unless (defined $numtoname{$_}) { my (@a) = gethostbyaddr(pack('C4', split(/\./)),2); $numtoname{$_} = @a > 0 ? $a[0] : $_; } return $numtoname{$_}; }

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:

package 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} ); }
Man I feel like an idiot for not seeing it right off the bat... grrr... must be lack of caffeine - that's it

In reply to local variables and classes by jfroebe

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.