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

hi, all:

I am trying to turn asic ip address like "172.16.249.232" into an int or long integer numeric, and I use the module NetAddr::IP::Util, but failed, it seems that the inet_aton() doesn't returns a numeric value

The simple code like this:
#!/usr/bin/perl use strict; use warnings; use NetAddr::IP::Util qw(:all :inet :ipv4 :ipv6 :math); my $netaddr = inet_aton( '172.16.249.232' ); print "net_addr:", $netaddr, "\n"; printf "net_addrHex:%x\n", $netaddr;
and, gives the following messages:
net_addr:▒▒▒ Argument "▒^P▒▒" isn't numeric in printf at /foo.pl +line 10. net_addrHex:0

Replies are listed 'Best First'.
Re: Does inet_aton() return a numeric value?
by JavaFan (Canon) on Nov 21, 2008 at 10:45 UTC
    Indeed, it doesn't. Assumong NetAddr::IP::Util::inet_aton works the same as Socket::inet_aton, and the inet_aton of the C library, it returns a packed (C) struct. Which typically consists of an integer. So, you should be able to easily unpack it:
    $ perl -MSocket -wE 'say unpack "N", inet_aton ("127.0.0.1");' 2130706433 $ echo '127 * 256^3 + 1' | bc 2130706433 $
Re: Does inet_aton() return a numeric value?
by Anonymous Monk on Nov 21, 2008 at 18:08 UTC
    $netaddr = inet_aton($dotquad); Convert a dot-quad IP address into an IPv4 packed network address. inet_aton HOSTNAME Takes a string giving the name of a host, and translates that to t +he 4-byte string (structure). Takes arguments of both the 'rtfm.mit.e +du' type and '18.181.0.24'. If the host name cannot be resolved, retu +rns undef.