in reply to gethostbyname bug?

This worked for me:
use strict; my @returnVal = gethostbyname "www.perlmonks.org"; my @packedIP = $returnVal[4]; my ($a, $b, $c, $d); my @a = ($a, $b, $c, $d) = unpack('C4', $packedIP[0]); my $ip = "$a.$b.$c.$d"; print $ip; exit(0);


Perhaps your problem was that you didn't grab the packed data from the second level array. (Caveat: I barely knew what I was doing in this situation.)

redmist
redmist.dyndns.org
redmist@altavista.net

Replies are listed 'Best First'.
RE: Re: gethostbyname bug?
by BastardOperator (Monk) on Sep 09, 2000 at 05:31 UTC
    You shouldn't need to do this. gethostbyname() should in fact return only the packed ip when referenced as a scalar
    my $packedIP = gethostbyname("www.perlmonks.org"); print join('.', unpack("C4", $packedIP)), "\n";
    This should work like a champ, although (myself and OzzyOzbourne were talking about this in the chatterbox), someone (tilly?) mentioned that in 5.6 the unpack isn't necessary. I can't confirm this though.
        Not ugly, just different. That's why Perl rules. TMTOWTDI ;)
      This was my ending code on this. $input was a standard IP...
      @ip = split(/\./, $input); $newname=gethostbyaddr(pack("C4", @ip), AF_INET);
      -Ozzyosbourne
RE: Re: gethostbyname bug?
by geektron (Curate) on Sep 09, 2000 at 05:28 UTC
    no, that way worked for me, too. (but i had a join ',', unpack('C4', @packed_ip); instead of the temp variables.

    i wasn't getting the packed data at all, for some reason. the call to  gethostbyname was only returning the first value ( which is name, if all args are requested, but address if called in scalar context according to the docs.)

    and, as the update says, now it's working as expected. perl magic - ask someone why things don't work, and they start working! kinda like taking the car to the mechanic. . . :) UPDATE: typo - the join had a dot, not a comma.