Funkster has asked for the wisdom of the Perl Monks concerning the following question:

Any correlation between me and reality, let alone my opinions and those of my employer, is entirely coincidental
I'm not worthy, I'm not worthy...but I do believe in the interconnectedness of all things...
I have a database that keeps ip addresses stored as 32 bit integers. I have to extract this and make an ascii string for a config file. If this were C, i'd stuff the address in the appropriate part of in_addr, and call inet_ntoa. Can't see how to get into the structures for Perl.. and all the references i find to inet_ntoa on web searches either
a. get a value using gethostbyname (not applicable)
b. talk about opaque values (which I don't understand)
c. seem to be using a structure, but are not clear how one manipulates the structure.
Here's what I'm trying to do:
use Socket; #================================================================= $var=0x0a0a0a01; #simulate the value from the database printf(STDERR "DEBUG, var is %4.4x \n", $var); print STDERR "DEBUG, var is $var\n"; $peer_addr = inet_ntoa($var); print "DEBUG, $peer_addr";
which gives the infamous output of:
DEBUG, var is a0a0a01 DEBUG, var is 168430081 Bad arg length for Socket::inet_ntoa, length is 9, should be 4
9. Hmmm is it relevant that that is the number of digits in the "print" value.. is it being stored in some format other than integer?

Replies are listed 'Best First'.
Re: inet_ntoa question
by esskar (Deacon) on Mar 04, 2004 at 20:46 UTC
    print inet_ntoa(pack("N", $var));
Re: inet_ntoa question
by Paladin (Vicar) on Mar 04, 2004 at 20:50 UTC
    It is being stored as an int. The problem is that inet_ntoa takes a 4 character string as an argument. You need to do something like:
    $var=chr(0x0a).chr(0x0a).chr(0x0a).chr(0x01); # OR $var=v10.10.10.1; #using the v-string notation if your perl is new en +ough # OR $var=pack "CCCC", 10, 10, 10, 1; # OR (probably best yet) $var=inet_aton 0x0a0a0a01;
    To get the proper string format to feed to inet_ntoa
      Any correlation between me and reality, let alone my opinions and those of my employer, is entirely coincedental
      Thankx, guys. I have a working solution.. (inet_ntoa(pack). and info that wasn't clear ntoa takes 4 chars). next task is to read up on pack and integer representation.... it's not clear to an old C guy why integer is different than the 4 concatenated characters you suggest.... if I cat the 4 1 byte chars x0a. 0xa . 0xa . 0x01 how is that different than the 32 bit value 0A0A0A01 that I had originally? mu la mi. guess I need to RTFM to figure out what really need to ask.