Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Contact Us
Online Help
Domain Status
Man Pages

Virtual Servers

Topology Map

Server Agreement
Year 2038

USA Flag



Man Pages

Manual Reference Pages  -  NET::IPADDRESS (3)

.ds Aq ’


Net::IPAddress - Functions used to manipulate IP addresses, masks and FQDN’s.



use Net::IPAddress;

@ISA = qw(Net::IPAddress);


Net::IPAddr is a collection of helpful functions used to convert IP addresses to/from 32-bit integers, applying subnet masks to IP addresses, validating IP address strings, and splitting a FQDN into its host and domain parts.

No rocket science here, but I have found these functions to very, very handy. For example, have you ever tried to sort a list of IP addresses only to find out that they don’t sort the way you expected? Here is the solution! If you convert the IP addresses to 32-bit integer addresses, they will sort in correct order.
ip2num( STRING ) Returns the 32-bit integer of the passed IP address string.

$ipnum = ip2num(""); $ipnum is 167837953.

num2ip( INTEGER ) Returns the IP address string of the passed 32-bit IP address.

$IP = num2ip(167837953); $IP is

validaddr( STRING ) Returns true (1) if the IP address string is a valid and properly formatted IP address, and false (0) otherwise.

$valid = validaddr(""); # returns true

$valid = validaddr("10.1.2."); # returns false!

If you have your own IP address validator, try the last one. Most will incorrectly compute that as a valid address.

mask( IPADDRESS, MASK ) Returns the result of binary (IPADDRESS & MASK). IPADDRESS can be either an IP address string or a 32-bit integer address. MASK can be either an IP address string, or the number of bits in the mask. The returned value will be in the same format as the passed IP address. If you pass an IP address string, then an IP address string is returned, if you pass a 32-bit integer address then a 32-bit integer address is returned.


  $subnet = mask("",16); # $subnet =

$subnet = mask("",""); # $subnet =

$subnet = mask(167837953,""); # $subnet = 167837952>

This function, when used with the others, is very useful for computing IP addresses. For example, you need to add another server to a subnet that an existing server is on. You want the new server to be the .17 address of a /24 subnet. This is done easily in the following example:

  use Net::IPAddress

$server = ""; $newserver = num2ip(ip2num(mask($server,24)) + 17); print "New server IP is $newserver\n";

New server IP is

The following code does exactly the same thing:

use Net::IPAddress;

$server = ""; $newserver = num2ip(mask(ip2num($server),24) + 17); print "New server IP is $newserver\n";

fqdn( FQDN ) This function returns the host and domain of the passed FQDN (fully qualified domain name).

($host,$domain) = fqdn(""); # $host = www, $domain =


Net::IPAddress exports five functions ip2num, num2ip, validaddr, mask, and fqdn.


Scott Renner <>, <>


Copyright(c) 2003-2005 Scott Renner. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Search for    or go to Top of page |  Section 3 |  Main Index

perl v5.20.3 IPADDRESS (3) 2005-10-25

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with manServer 1.07.