Using unpack here seems a bit awkward. Why not use the builtins from Socket and simplify?
use Socket; my $hostname = 'yahoo.com'; print inet_ntoa(inet_aton($hostname)),"\n";
inet_aton converts from "ASCII" to "Network", and as a bonus will resolve host names. The reverse, inet_ntoa, will do the inverse, though it will always give you a dotted IP and not the name. To find that out, you will have to use Net::DNS.

You will note that it puts out different answers on occasion, though, because there are many possible addresses to choose from, and they are selected at random, which is called "Round Robin DNS".

As a side note, this code will die "Bad arg length..." if fed an invalid or unresolvable address. This can be fixed by splitting it into two steps and verifying that inet_aton() actually returned something.

In reply to Re^3: gethostbyname not working by tadman
in thread gethostbyname not working by nick

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.