NAME
NetAddr::IP::Constants - Magic number constants for NetAddr::IP
VERSION
version 4.080_05
SYNOPSIS
use NetAddr::IP::Constants qw($IPV6_BITS $IPV4_BITS $IPV4_OFFSET);
print "$IPV6_BITS\n"; # 128
print "$IPV4_BITS\n"; # 32
print "$IPV4_OFFSET\n"; # 96
print "$NetAddr::IP::Constants::IPV6_BITS\n"; # 128, same variable
DESCRIPTION
Provides named constants for the magic numbers used throughout the NetAddr::IP family of modules. Importing by name avoids polluting the caller's namespace with abbreviations.
These are our package variables and are read at runtime, not resolved at compile time, so importing one does not freeze its value. Assigning to an imported name changes what that name returns for the rest of the program:
use NetAddr::IP::Constants qw($IPV6_BITS);
print "$IPV6_BITS\n"; # 128
$IPV6_BITS = 999;
print "$IPV6_BITS\n"; # 999
The import is an alias, not a copy, so an assignment to the imported name writes through to the module's own variable, which is what the rest of the distribution reads:
print "$NetAddr::IP::Constants::IPV6_BITS\n"; # 999
Which is why reassigning an imported constant changes the library's own behaviour, not just the caller's view of it.
An our variable of the same name in the importing package is replaced by the import, and a lexical my variable of that name hides it. Every module in the distribution that reads one of these names imports it the same way, so a reassignment through any imported name, or through the full name in NetAddr::IP::Constants, reaches them all. After $IPV6_BITS = 999, the length error from isIPv4 reads should be 999 on both builds, and so does the one from hasbits in a pure Perl build, while the XS functions compile their lengths in and keep their own. The objects' own arithmetic breaks too: NetAddr::IP->new('2001:db8::/64')->masklen then dies in shiftleft. The NetAddr::IP::Util module imports only $V4_PACKED_BYTES and $V6_PACKED_BYTES, so assigning $NetAddr::IP::Util::IPV6_BITS changes nothing. Overriding a constant is reachable but unsupported.
CONSTANTS
IPv4 address geometry, RFC 791
- $IPV4_BITS
-
IPv4 address width in bits. Value:
32. RFC 791 s2.1. - $MAX_OCTET
-
Maximum value of a single IPv4 octet. Value:
255(0xff). RFC 791 s2.1. - $OCTET_BITS
-
Number of bits in a single IPv4 octet. Value:
8. RFC 791 s2.1. - $OCTET_COUNT
-
Number of octets in an IPv4 address. Value:
4. RFC 791 s2.1. - $V4_PACKED_BYTES
-
Size of a packed IPv4 address in bytes. Value:
4(32/8). RFC 791 s2.1.
IPv6 address geometry, RFC 4291
- $IPV6_BITS
-
IPv6 address width in bits. Value:
128. RFC 4291 s2.1. - $V6_PACKED_BYTES
-
Size of a packed IPv6 address in bytes. Value:
16(128/8). RFC 4291 s2.1. - $IPV4_OFFSET
-
Offset of the IPv4-in-IPv6 representation in the 128 bit form, that is
$IPV6_BITS - $IPV4_BITS. Value:96.RFC 4291 s2.5.5.1 defines the IPv4-compatible prefix
::/96, which is deprecated, and s2.5.5.2 the IPv4-mapped prefix::ffff:0:0/96. Both place the 32 bit IPv4 address at this offset, which is why the same offset serves both.
Point-to-point networks, RFC 3021
- $RFC3021_THRESHOLD
-
Networks of length /127 or /31 are treated as having two usable addresses, per RFC 3021. Value:
127(i.e.$IPV6_BITS - 1).
Netlimit exponents
- $DEFAULT_NETLIMIT_EXP
-
Power-of-two exponent for the default netlimit,
2**16 = 65536networks. Value:16. - $MAX_NETLIMIT_EXP
-
Power-of-two exponent for the maximum netlimit,
2**24 = 16777216networks. Value:24. - $MAX_SHIFTLEFT
-
Maximum number of bits
shiftleft()can shift. Value:128, that is$IPV6_BITS.
Binary coded decimal
- $MAX_BCD_DIGITS
-
Width of a packed BCD string, in hex nibbles. Value:
40.The width follows from the value range rather than from the digit count alone. The largest 128 bit value needs 39 decimal digits, and BCD holds one digit per nibble, but a packed string has to be a whole number of bytes, so 39 nibbles round up to 40, which is 20 bytes.
bin2bcd()returns 39 digits unpadded, andsimple_pack()is what pads to 40 beforepack("H40"). - $PACKED_BCD_BYTES
-
Size of a packed BCD string in bytes. Value:
20($MAX_BCD_DIGITS/2, two digits per byte).
EXPORTS
Nothing is exported by default. Use explicit import tags:
use NetAddr::IP::Constants qw(:all);
or import individual constants:
use NetAddr::IP::Constants qw($IPV6_BITS $IPV4_BITS);
AUTHORS
Dean Hamstead <dean@fragfest.com.au>
Luis E. Muñoz <luismunoz@cpan.org>
Michael Robinton <michael@bizsystems.com>
COPYRIGHT AND LICENSE
This software is Copyright (c) 2026 by Dean Hamstead, Luis E. Muñoz, Michael Robinton.
This is free software, licensed under:
The MIT (X11) License
The full text of the license can be found in the LICENSE file included with this distribution.