Let's start with this:
gethostbyaddr(192.168.1.1, AF_INET);
The fact that this works is sort of an accident. You're "supposed" to do something more like this:
$ip = inet_aton('192.168.1.1'); print gethostbyaddr($ip, AF_INET);
Your example appears to work as a side effect of a new string syntax intended to support unicode (and software version numbers like "5.6.0"). It does the right thing in your example but it doesn't appear to have been designed with IP addresses in mind.

Okay, but why does it fail in your second block of code? When perl is compiling your code and encounters "192.168.1.1" as a bare word, it packs the four bytes 192, 168, 1, and 1 into a four-characters string (this happens to be the same thing that inet_aton() returns, and that gethostbyaddr() is expecting to receive). This conversion process is part of the perl compilation process, i.e. it happens it compile time.

In your second example, you're asking for the same thing to happen at runtime. This isn't going to work. gethostbyaddr() is expecting to receive a four-byte string containing a packed IP address; instead it gets an 11-byte string containing a literal "192.168.1.1" (or similar).

Further, in your second example, you're using 'tcp' as the second argument to gethostbyaddr(). This isn't correct. You should supply AF_INET as you did in your first example.

In your second example, try replacing the gethostbyaddr() line with the following:

my $ip = inet_aton($&); my $name = gethostbyaddr($ip, AF_INET);

In reply to Re: novice falls by kjherron
in thread novice falls by ddeyoung

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.