in reply to IP to numeric value.

The Socket module provides inet_aton and inet_ntoa, which wrap your network library.

I'd write your function like this:

use Socket qw/ inet_aton /; sub ipToNum { my $ip = shift; return unpack "N", inet_aton( $ip ); }

If your database is MySQL, it also has native versions of those functions, so you could run queries like

select inet_ntoa( ipnum ) from iptable

Replies are listed 'Best First'.
Re^2: IP to numeric value.
by cerror (Scribe) on Jul 12, 2007 at 01:16 UTC
    Ah, thank you.

    I'm still rather new to perl, so I guess I inadvertently spend a little time reinventing the wheel.

    Though, I suppose it's always a good exercise to figure out how those sort of functions work on your own.

    - cerror
      There's nothing wrong with reinventing the wheel, so long as you're know you're doing it, and are clear on the reasons why you're doing it. To learn how something works is a pretty good reason.
      Absolutely, what Mutant said. It's always good to practice. Bit-manipulation functions are low-level enough that it takes some time to get them committed to memory.

      As for reinventing the wheel, you don't always realise you're doing it until someone else points out the existing ones.

      As a matter of fact, I just discovered an ip2num function in one of my own libraries (written about 3 years ago). I guess I didn't know about those native functions back then either :-)