Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: IP address - long to dottedquad to long

by davidrw (Prior)
on Apr 28, 2006 at 20:20 UTC ( [id://546385]=note: print w/replies, xml ) Need Help??


in reply to IP address - long to dottedquad to long

Net::IP solutions:
# for just DottedQuadToLong(): use Net::IP; print Net::IP->new("24.72.30.83")->intip; # for both directions: use Net::IP qw/:PROC/; print ip_bintoint(ip_iptobin("24.72.30.83",4)); print ip_bintoip(ip_inttobin("407379539",4),4);

couple code comments:
  • Personnally i'd write foreach (@quads) { $_ = hex } w/map: return join '.', map {hex} @quads;
  • in DottedQuadToLong, the split is just extra work -- just capture each piece (don't need the substitution, either)
    sub DottedQuadToLong { my $ip_str = shift; return 0 unless $ip_str =~ /^\s*(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.( +\d{1,3})\s*$/; my $ip = 0; foreach my $quad ($1,$2,$3,$4){ return 0 if ($quad > 255); $ip = $ip * 256 + $quad; } return $ip; }

Replies are listed 'Best First'.
Re^2: IP address - long to dottedquad to long
by ruzam (Curate) on Apr 28, 2006 at 20:36 UTC
    Nice tips. I rarely use map (it scares me) so I never would have thought of trying that. The spilt I should have seen coming...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://546385]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-03-28 23:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found