Dave Rolsky > Net-Works-0.05 > Net::Works::Network



Annotate this POD


Open  0
View/Report Bugs
Module Version: 0.05   Source   Latest Release: Net-Works-0.18


Net::Works::Network - An object representing a single IP address (4 or 6) subnet


version 0.05


  my $network = Net::Works::Network->new_from_string( string => '' );
  print $network->as_string();          #
  print $network->mask_length();        # 24
  print $network->bits();               # 32
  print $network->version();            # 4

  my $first = $network->first();
  print $first->as_string();    #

  my $last = $network->first();
  print $last->as_string();     #

  my $iterator = $network->iterator();
  while ( my $ip = $iterator->() ) { ... }

  my $network = Net::Works::Network->new_from_string( string => '' );
  print $network->max_mask_length(); # 30

  # All methods work with IPv4 and IPv6 subnets
  my $network = Net::Works::Network->new_from_string( string => 'a800:f000::/20' );

  my @subnets = Net::Works::Network->range_as_subnets( '', '' );
  print $_->as_string, "\n" for @subnets;


Objects of this class represent an IP address network. It can handle both IPv4 and IPv6 subnets. It provides various methods for getting information about the subnet.

For IPv6, it uses big integers (via Math::BigInt) to represent the numeric value of an address as needed.

This module is currently a thin wrapper around NetAddr::IP but that could change in the future.


This class provides the following methods:

Net::Works::Network->new_from_string( ... )

This method takes a string parameter and an optional version parameter. The string parameter should be a string representation of an IP address subnet.

The version parameter should be either 4 or 6, but you don't really need this unless you're trying to force a dotted quad to be interpreted as an IPv6 network or to a force an IPv6 address colon-separated hex number to be interpreted as an IPv4 network.

Net::Works::Network->new_from_integer( ... )

This method takes an integer parameter and an optional version parameter. The integer parameter should be an integer representation of an IP within the subnet. The version parameter should be either 4 or 6.


Returns a string representation of the network like "" or "a800:f000::/105". The IP address in the string is the first address within the subnet.


Returns a 4 or 6 to indicate whether this is an IPv4 or IPv6 network.


Returns the length of the netmask as an integer.


Returns the number of bit of an address in the network, which is either 32 (IPv4) or 128 (IPv6).


This returns the maximum possible numeric subnet that this network could fit in. In other words, the subnet could be part of the subnet, so this returns 24.


Returns the first IP in the network as an Net::Works::Address object.


Returns the last IP in the network as an Net::Works::Address object.


This returns an anonymous sub that returns one IP address in the range each time it's called.

For single address subnets (/32 or /128), this returns a single address.

When it has exhausted all the addresses in the network, it returns undef

Net::Works::Network->range_as_subnets( $first, $last )

Given two IP addresses as strings, this method breaks the range up into the largest subnets that include all the IP addresses in the range (including the two passed to this method).

It also excludes any reserved subnets in the range (such as the or ranges).

This method works with both IPv4 and IPv6 addresses. If either address contains a colon (:) then it assumes that you want IPv6 subnets.



This software is Copyright (c) 2012 by MaxMind, Inc..

This is free software, licensed under:

  The Artistic License 2.0 (GPL Compatible)
syntax highlighting: