Tie::NetAddr::IP - Implements a Hash where the key is a subnet


  use Tie::NetAddr::IP;

  my %WhereIs;
  tie %WhereIs, Tie::NetAddr::IP;

  $WhereIs{""} = "Lab, First Floor";
  $WhereIs{""} = "Datacenter, Second Floor";
  $WhereIs{""} = "Remote location";
  $WhereIs{""} = "God knows where";

  foreach $host ("", "", "", "") {
     print "Host $host is in ", $WhereIs{$host}, "\n";

  foreach $subnet (keys %WhereIs) {
     print "Network ", $subnet, " is used in ", 
     $WhereIs{$subnet}, "\n";

  untie %WhereIs;


This module overloads hashes so that the key can be a subnet as in NetAddr::IP. When looking values up, an interpretation will be made to find the given key within the subnets specified in the hash.

The code sample provided on the SYNOPSIS would print out the locations of every machine in the foreach loop.

Care must be taken, as only strings that can be parsed as an IP address by NetAddr::IP can be used as keys for this hash.

Iterators on the hash such as foreach, each, keys and values will only see the actual subnets provided as keys to the hash. When looking up a value such as in $hash{$ipaddress} this IP address will be looked up among the subnets existing as keys within the hash. The matching subnet with the longest mask (ie, the most specific subnet) will win and its associated value will be returned.

This code can be distributed freely according to the terms set forth in the PERL license provided that proper credit is maintained. Please send bug reports and feedback to the author for further improvement.



original version; created by h2xs 1.19


Renamed to Tie::NetAddr::IP to match the modulelist name


Modified to use NetAddr::IP v3.00. Added a number of new tests


General update. Patch from Kazuyuki Maejima to fix bug related to keys, next, each, etc.


Luis E. Muñoz (


perl(1), NetAddr::IP(3).

1 POD Error

The following errors were encountered while parsing the POD:

Around line 216:

Non-ASCII character seen before =encoding in 'Muñoz'. Assuming CP1252