in reply to IP operation trouble
Untested, season with error-checking to taste.sub ipv4_to_int { my $int = 0; foreach (split(/\./, $_[0]) { $int <<= 8; $int += $_; } return $int; } sub int_to_ipv4 { my $ip = ''; my $int = shift; while($int) { $ip = ($int & 255).".$ip"; $int >>= 8; } chop($ip); return $ip; }
You hardly ever see the >> and << bit-shifting operators in perl, and bitwise AND (&) is rare too, but they behave exactly the same as they do in C.
And while I normally disapprove of "magic numbers" like the 8 and 255, I think that in this case their meaning is sufficiently obvious.
|
|---|