Here's a common recipe for converting IP addresses in dotted-quad notation (192.168.0.1 for example) to an integer number and converting back again. I always seem to lose this recipe when I need it so I'll jot it down here. This is useful for storing an IP address, subnet, or subnet mask in a database as you can store it in a 4 byte integer field rather than a 15 byte char, a savings of 11 bytes per record (by MySQL's data types).
sub quad2int
{
return unpack("N",pack("C4",split('\.',$_[0])));
}
sub int2quad
{
return join('.',unpack('C4', pack("N", $_[0])));
}
Usage: my $int = quad2int('192.168.0.1'); OR my $ip = int2quad(3232235521);
I yanked this code from the fantastic Perl module Net::Netmask by David Muir Sharnoff