in reply to gethostbyname bug?

gethostbyname seems to return a packed ip address, in scalar context. You might use the Socket library to get the inet_ntoa function:
use Socket; my $packed_ip = gethostbyname('slashdot.org'); my $ip = inet_ntoa($packed_ip); print "$ip\n";

Update: The parenthesis don't matter in this case. I usually use them to avoid precedence misunderstandings, though.

Replies are listed 'Best First'.
RE: Re: gethostbyname bug?
by geektron (Curate) on Sep 09, 2000 at 04:53 UTC
    so do the parens make the difference? (silly question, but i've had issues w/ parens and precedence before).

    w/out divulging the actual hostnames, here's what i've gotten:

    my $ip = gethostbyname foo.bar.net; print $ip;

    gives me back 'foo.bar.net'

    UPDATE: now it's working. i don't get it. i've been agonizing over this for hours (and all my previous attempts have never worked.)

      You were probably doing something like this:
      my ($ip) = gethostbyname("slashdot.org");

      In that code, $ip is in a list, and the statement is in list context, so the statement is equivalent to:

      my ($ip, undef, undef, undef, undef) = gethostbyname("slashdot.org");

      which will put the name in returned from gethostbyname in $ip.

      Aside: This is the reason I rail against unnecessary parenthisation in perl programs---there can be nasty context bugs until you get used to it.

        actually, i wasn't. the code i posted was what i was using. i completely understand what the extra parens would have done (forcing list context, then the first element of that list would be returned).