{
    "mode": "perldoc",
    "parameter": "Net::IP",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Net%3A%3AIP/json",
    "generated": "2026-06-10T02:38:14Z",
    "synopsis": "use Net::IP;\nmy $ip = new Net::IP ('193.0.1/24') or die (Net::IP::Error());\nprint (\"IP  : \".$ip->ip().\"\\n\");\nprint (\"Sho : \".$ip->short().\"\\n\");\nprint (\"Bin : \".$ip->binip().\"\\n\");\nprint (\"Int : \".$ip->intip().\"\\n\");\nprint (\"Mask: \".$ip->mask().\"\\n\");\nprint (\"Last: \".$ip->lastip().\"\\n\");\nprint (\"Len : \".$ip->prefixlen().\"\\n\");\nprint (\"Size: \".$ip->size().\"\\n\");\nprint (\"Type: \".$ip->iptype().\"\\n\");\nprint (\"Rev:  \".$ip->reverseip().\"\\n\");",
    "sections": {
        "NAME": {
            "content": "Net::IP - Perl extension for manipulating IPv4/IPv6 addresses\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use Net::IP;\n\nmy $ip = new Net::IP ('193.0.1/24') or die (Net::IP::Error());\nprint (\"IP  : \".$ip->ip().\"\\n\");\nprint (\"Sho : \".$ip->short().\"\\n\");\nprint (\"Bin : \".$ip->binip().\"\\n\");\nprint (\"Int : \".$ip->intip().\"\\n\");\nprint (\"Mask: \".$ip->mask().\"\\n\");\nprint (\"Last: \".$ip->lastip().\"\\n\");\nprint (\"Len : \".$ip->prefixlen().\"\\n\");\nprint (\"Size: \".$ip->size().\"\\n\");\nprint (\"Type: \".$ip->iptype().\"\\n\");\nprint (\"Rev:  \".$ip->reverseip().\"\\n\");\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "This module provides functions to deal with IPv4/IPv6 addresses. The module can be used as a\nclass, allowing the user to instantiate IP objects, which can be single IP addresses, prefixes,\nor ranges of addresses. There is also a procedural way of accessing most of the functions. Most\nsubroutines can take either IPv4 or IPv6 addresses transparently.\n",
            "subsections": []
        },
        "OBJECT-ORIENTED INTERFACE": {
            "content": "",
            "subsections": [
                {
                    "name": "Object Creation",
                    "content": "A Net::IP object can be created from a single IP address:\n\n$ip = new Net::IP ('193.0.1.46') || die ...\n\nOr from a Classless Prefix (a /24 prefix is equivalent to a C class):\n\n$ip = new Net::IP ('195.114.80/24') || die ...\n\nOr from a range of addresses:\n\n$ip = new Net::IP ('20.34.101.207 - 201.3.9.99') || die ...\n\nOr from a address plus a number:\n\n$ip = new Net::IP ('20.34.10.0 + 255') || die ...\n\nThe new() function accepts IPv4 and IPv6 addresses:\n\n$ip = new Net::IP ('dead:beef::/32') || die ...\n\nOptionally, the function can be passed the version of the IP. Otherwise, it tries to guess what\nthe version is (see isipv4() and isipv6()).\n\n$ip = new Net::IP ('195/8',4); # Class A\n"
                }
            ]
        },
        "OBJECT METHODS": {
            "content": "Most of these methods are front-ends for the real functions, which use a procedural interface.\nMost functions return undef on failure, and a true value on success. A detailed description of\nthe procedural interface is provided below.\n\nset\nSet an IP address in an existing IP object. This method has the same functionality as the new()\nmethod, except that it reuses an existing object to store the new IP.\n\n\"$ip->set('130.23.1/24',4);\"\n\nLike new(), set() takes two arguments - a string used to build an IP address, prefix, or range,\nand optionally, the IP version of the considered address.\n\nIt returns an IP object on success, and undef on failure.\n\nerror\nReturn the current object error string. The error string is set whenever one of the methods\nproduces an error. Also, a global, class-wide Error() function is available.\n\n\"warn ($ip->error());\"\n\nerrno\nReturn the current object error number. The error number is set whenever one of the methods\nproduces an error. Also, a global $ERRNO variable is set when an error is produced.\n\n\"warn ($ip->errno());\"\n\nip\nReturn the IP address (or first IP of the prefix or range) in quad format, as a string.\n\n\"print ($ip->ip());\"\n\nbinip\nReturn the IP address as a binary string of 0s and 1s.\n\n\"print ($ip->binip());\"\n\nprefixlen\nReturn the length in bits of the current prefix.\n\n\"print ($ip->prefixlen());\"\n\nversion\nReturn the version of the current IP object (4 or 6).\n\n\"print ($ip->version());\"\n\nsize\nReturn the number of IP addresses in the current prefix or range. Use of this function requires\nMath::BigInt.\n\n\"print ($ip->size());\"\n\nbinmask\nReturn the binary mask of the current prefix, if applicable.\n\n\"print ($ip->binmask());\"\n\nmask\nReturn the mask in quad format of the current prefix.\n\n\"print ($ip->mask());\"\n\nprefix\nReturn the full prefix (ip+prefix length) in quad (standard) format.\n\n\"print ($ip->prefix());\"\n\nprint\nPrint the IP object (IP/Prefix or First - Last)\n\n\"print ($ip->print());\"\n\nintip\nConvert the IP in integer format and return it as a Math::BigInt object.\n\n\"print ($ip->intip());\"\n\nhexip\nReturn the IP in hex format\n\n\"print ($ip->hexip());\"\n\nhexmask\nReturn the mask in hex format\n\n\"print ($ip->hexmask());\"\n\nshort\nReturn the IP in short format: IPv4 addresses: 194.5/16 IPv6 addresses: ab32:f000::\n\n\"print ($ip->short());\"\n\niptype\nReturn the IP Type - this describes the type of an IP (Public, Private, Reserved, etc.) See\nprocedural interface ipiptype for more details.\n\n\"print ($ip->iptype());\"\n\nreverseip\nReturn the reverse IP for a given IP address (in.addr. format).\n\n\"print ($ip->reserveip());\"\n\nlastip\nReturn the last IP of a prefix/range in quad format.\n\n\"print ($ip->lastip());\"\n\nlastbin\nReturn the last IP of a prefix/range in binary format.\n\n\"print ($ip->lastbin());\"\n\nlastint\nReturn the last IP of a prefix/range in integer format.\n\n\"print ($ip->lastint());\"\n\nfindprefixes\nThis function finds all the prefixes that can be found between the two addresses of a range. The\nfunction returns a list of prefixes.\n\n\"@list = $ip->findprefixes($otherip));\"\n\nbincomp\nBinary comparaison of two IP objects. The function takes an operation and an IP object as\narguments. It returns a boolean value.\n\nThe operation can be one of: lt: less than (smaller than) le: smaller or equal to gt: greater\nthan ge: greater or equal to\n\n\"if ($ip->bincomp('lt',$ip2) {...}\"\n\nbinadd\nBinary addition of two IP objects. The value returned is an IP object.\n\n\"my $sum = $ip->binadd($ip2);\"\n\naggregate\nAggregate 2 IPs - Append one range/prefix of IPs to another. The last address of the first range\nmust be the one immediately preceding the first address of the second range. A new IP object is\nreturned.\n\n\"my $total = $ip->aggregate($ip2);\"\n\noverlaps\nCheck if two IP ranges/prefixes overlap each other. The value returned by the function should be\none of: $IPPARTIALOVERLAP (ranges overlap) $IPNOOVERLAP (no overlap) $IPAINBOVERLAP\n(range2 contains range1) $IPBINAOVERLAP (range1 contains range2) $IPIDENTICAL (ranges are\nidentical) undef (problem)\n\n\"if ($ip->overlaps($ip2)==$IPAINBOVERLAP) {...};\"\n\nlooping\nThe \"+\" operator is overloaded in order to allow looping though a whole range of IP addresses:\n\nmy $ip = new Net::IP ('195.45.6.7 - 195.45.6.19') || die;\n# Loop\ndo {\nprint $ip->ip(), \"\\n\";\n} while (++$ip);\n\nThe ++ operator returns undef when the last address of the range is reached.\n\nauth\nReturn IP authority information from the IP::Authority module\n\n\"$auth = ip-\"auth ();>\n\nNote: IPv4 only\n",
            "subsections": []
        },
        "PROCEDURAL INTERFACE": {
            "content": "These functions do the real work in the module. Like the OO methods, most of these return undef\non failure. In order to access error codes and strings, instead of using $ip->error() and\n$ip->errno(), use the global functions \"Error()\" and \"Errno()\".\n\nThe functions of the procedural interface are not exported by default. In order to import these\nfunctions, you need to modify the use statement for the module:\n\n\"use Net::IP qw(:PROC);\"\n",
            "subsections": [
                {
                    "name": "Error",
                    "content": "Returns the error string corresponding to the last error generated in the module. This is also\nuseful for the OO interface, as if the new() function fails, we cannot call $ip->error() and so\nwe have to use Error().\n\nwarn Error();\n"
                },
                {
                    "name": "Errno",
                    "content": "Returns a numeric error code corresponding to the error string returned by Error.\n\nipiptobin\nTransform an IP address into a bit string.\n\nParams  : IP address, IP version\nReturns : binary IP string on success, undef otherwise\n\n\"$binip = ipiptobin ($ip,6);\"\n\nipbintoip\nTransform a bit string into an IP address\n\nParams  : binary IP, IP version\nReturns : IP address on success, undef otherwise\n\n\"$ip = ipbintoip ($binip,6);\"\n\nipbintoint\nTransform a bit string into a BigInt.\n\nParams  : binary IP\nReturns : BigInt\n\n\"$bigint = new Math::BigInt (ipbintoint($binip));\"\n\nipinttobin\nTransform a BigInt into a bit string. *Warning*: sets warnings (\"-w\") off. This is necessary\nbecause Math::BigInt is not compliant.\n\nParams  : BigInt, IP version\nReturns : binary IP\n\n\"$binip = ipinttobin ($bigint);\"\n\nipgetversion\nTry to guess the IP version of an IP address.\n\nParams  : IP address\nReturns : 4, 6, undef(unable to determine)\n\n\"$version = ipgetversion ($ip)\"\n\nipisipv4\nCheck if an IP address is of type 4.\n\nParams  : IP address\nReturns : 1 (yes) or 0 (no)\n\n\"ipisipv4($ip) and print \"$ip is IPv4\";\"\n\nipisipv6\nCheck if an IP address is of type 6.\n\nParams            : IP address\nReturns           : 1 (yes) or 0 (no)\n\n\"ipisipv6($ip) and print \"$ip is IPv6\";\"\n\nipexpandaddress\nExpand an IP address from compact notation.\n\nParams  : IP address, IP version\nReturns : expanded IP address or undef on failure\n\n\"$ip = ipexpandaddress ($ip,4);\"\n\nipgetmask\nGet IP mask from prefix length.\n\nParams  : Prefix length, IP version\nReturns : Binary Mask\n\n\"$mask = ipgetmask ($len,6);\"\n\niplastaddressbin\nReturn the last binary address of a prefix.\n\nParams  : First binary IP, prefix length, IP version\nReturns : Binary IP\n\n\"$lastbin = iplastaddressbin ($ip,$len,6);\"\n\nipsplitprefix\nSplit a prefix into IP and prefix length. If it was passed a simple IP, it just returns it.\n\nParams  : Prefix\nReturns : IP, optionally length of prefix\n\n\"($ip,$len) = ipsplitprefix ($prefix)\"\n\nipprefixtorange\nGet a range of IPs from a prefix.\n\nParams  : Prefix, IP version\nReturns : First IP, last IP\n\n\"($ip1,$ip2) = ipprefixtorange ($prefix,6);\"\n\nipbincomp\nCompare binary Ips with <, >, <=, >=. Operators are lt(<), le(<=), gt(>), and ge(>=)\n\nParams  : First binary IP, operator, Last binary IP\nReturns : 1 (yes), 0 (no), or undef (problem)\n\n\"ipbincomp ($ip1,'lt',$ip2) == 1 or do {}\"\n\nipbinadd\nAdd two binary IPs.\n\nParams  : First binary IP, Last binary IP\nReturns : Binary sum or undef (problem)\n\n\"$binip = ipbinadd ($bin1,$bin2);\"\n\nipgetprefixlength\nGet the prefix length for a given range of 2 IPs.\n\nParams  : First binary IP, Last binary IP\nReturns : Length of prefix or undef (problem)\n\n\"$len = ipgetprefixlength ($ip1,$ip2);\"\n\niprangetoprefix\nReturn all prefixes between two IPs.\n\nParams  : First IP (binary format), Last IP (binary format), IP version\nReturns : List of Prefixes or undef (problem)\n\nThe prefixes returned have the form q.q.q.q/nn.\n\n\"@prefix = iprangetoprefix ($ip1,$ip2,6);\"\n\nipcompressv4prefix\nCompress an IPv4 Prefix.\n\nParams  : IP, Prefix length\nReturns : Compressed Prefix\n\n\"$ip = ipcompressv4prefix ($ip, $len);\"\n\nipcompressaddress\nCompress an IPv6 address. Just returns the IP if it is an IPv4.\n\nParams  : IP, IP version\nReturns : Compressed IP or undef (problem)\n\n\"$ip = ipcompressadress ($ip, $version);\"\n\nipisoverlap\nCheck if two ranges of IPs overlap.\n\nParams  : Four binary IPs (begin of range 1,end1,begin2,end2), IP version\n$IPPARTIALOVERLAP (ranges overlap)\n$IPNOOVERLAP      (no overlap)\n$IPAINBOVERLAP  (range2 contains range1)\n$IPBINAOVERLAP  (range1 contains range2)\n$IPIDENTICAL       (ranges are identical)\nundef               (problem)\n\n\"(ipisoverlap($rb1,$re1,$rb2,$re2,4) eq $IPAINBOVERLAP) and do {};\"\n\nipgetembeddedipv4\nGet an IPv4 embedded in an IPv6 address\n\nParams  : IPv6\nReturns : IPv4 string or undef (not found)\n\n\"$ip4 = ipgetembedded($ip6);\"\n\nipcheckmask\nCheck the validity of a binary IP mask\n\nParams  : Mask\nReturns : 1 or undef (invalid)\n\n\"ipcheckmask($binmask) or do {};\"\n\nChecks if mask has only 1s followed by 0s.\n\nipaggregate\nAggregate 2 ranges of binary IPs\n\nParams  : 1st range (1st IP, Last IP), last range (1st IP, last IP), IP version\nReturns : prefix or undef (invalid)\n\n\"$prefix = ipaggregate ($bip1,$eip1,$bip2,$eip2) || die ...\"\n\nipiptypev4\nReturn the type of an IPv4 address.\n\nParams:  binary IP\nReturns: type as of the following table or undef (invalid ip)\n\nSee RFC 5735 and RFC 6598\n\nAddress Block       Present Use                Reference\n-------------------------------------------------------------------\n0.0.0.0/8           \"This\" Network             RFC 1122 PRIVATE\n10.0.0.0/8          Private-Use Networks       RFC 1918 PRIVATE\n100.64.0.0/10       CGN Shared Address Space   RFC 6598 SHARED\n127.0.0.0/8         Loopback                   RFC 1122 LOOPBACK\n169.254.0.0/16      Link Local                 RFC 3927 LINK-LOCAL\n172.16.0.0/12       Private-Use Networks       RFC 1918 PRIVATE\n192.0.0.0/24        IETF Protocol Assignments  RFC 5736 RESERVED\n192.0.2.0/24        TEST-NET-1                 RFC 5737 TEST-NET\n192.88.99.0/24      6to4 Relay Anycast         RFC 3068 6TO4-RELAY\n192.168.0.0/16      Private-Use Networks       RFC 1918 PRIVATE\n198.18.0.0/15       Network Interconnect\nDevice Benchmark Testing   RFC 2544 RESERVED\n198.51.100.0/24     TEST-NET-2                 RFC 5737 TEST-NET\n203.0.113.0/24      TEST-NET-3                 RFC 5737 TEST-NET\n224.0.0.0/4         Multicast                  RFC 3171 MULTICAST\n240.0.0.0/4         Reserved for Future Use    RFC 1112 RESERVED\n255.255.255.255/32  Limited Broadcast          RFC 919  BROADCAST\nRFC 922\n\nipiptypev6\nReturn the type of an IPv6 address.\n\nParams:  binary ip\nReturns: type as of the following table or undef (invalid)\n\nSee IANA Internet Protocol Version 6 Address Space\n<http://www.iana.org/assignments/ipv6-address-space/ipv6-address-space.txt> and IANA IPv6\nSpecial Purpose Address Registry\n<http://www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.txt>\n\nPrefix      Allocation           Reference\n-------------------------------------------------------------\n0000::/8    Reserved by IETF     [RFC4291] RESERVED\n0100::/8    Reserved by IETF     [RFC4291] RESERVED\n0200::/7    Reserved by IETF     [RFC4048] RESERVED\n0400::/6    Reserved by IETF     [RFC4291] RESERVED\n0800::/5    Reserved by IETF     [RFC4291] RESERVED\n1000::/4    Reserved by IETF     [RFC4291] RESERVED\n2000::/3    Global Unicast       [RFC4291] GLOBAL-UNICAST\n4000::/3    Reserved by IETF     [RFC4291] RESERVED\n6000::/3    Reserved by IETF     [RFC4291] RESERVED\n8000::/3    Reserved by IETF     [RFC4291] RESERVED\nA000::/3    Reserved by IETF     [RFC4291] RESERVED\nC000::/3    Reserved by IETF     [RFC4291] RESERVED\nE000::/4    Reserved by IETF     [RFC4291] RESERVED\nF000::/5    Reserved by IETF     [RFC4291] RESERVED\nF800::/6    Reserved by IETF     [RFC4291] RESERVED\nFC00::/7    Unique Local Unicast [RFC4193] UNIQUE-LOCAL-UNICAST\nFE00::/9    Reserved by IETF     [RFC4291] RESERVED\nFE80::/10   Link Local Unicast   [RFC4291] LINK-LOCAL-UNICAST\nFEC0::/10   Reserved by IETF     [RFC3879] RESERVED\nFF00::/8    Multicast            [RFC4291] MULTICAST\n\nPrefix          Assignment            Reference\n---------------------------------------------------------------------\n::1/128         Loopback Address      [RFC4291] UNSPECIFIED\n::/128          Unspecified Address   [RFC4291] LOOPBACK\n::FFFF:0:0/96   IPv4-mapped Address   [RFC4291] IPV4MAP\n0100::/64       Discard-Only Prefix   [RFC6666] DISCARD\n2001:0000::/32  TEREDO                [RFC4380] TEREDO\n2001:0002::/48  BMWG                  [RFC5180] BMWG\n2001:db8::/32   Documentation Prefix  [RFC3849] DOCUMENTATION\n2001:10::/28    ORCHID                [RFC4843] ORCHID\n2002::/16       6to4                  [RFC3056] 6TO4\nFC00::/7        Unique-Local          [RFC4193] UNIQUE-LOCAL-UNICAST\nFE80::/10       Linked-Scoped Unicast [RFC4291] LINK-LOCAL-UNICAST\nFF00::/8        Multicast             [RFC4291] MULTICAST\n\nipiptype\nReturn the type of an IP (Public, Private, Reserved)\n\nParams  : Binary IP to test, IP version (defaults to 6)\nReturns : type (see ipiptypev4 and ipiptypev6 for details) or undef (invalid)\n\n\"$type = ipiptype ($ip);\"\n\nipcheckprefix\nCheck the validity of a prefix\n\nParams  : binary IP, length of prefix, IP version\nReturns : 1 or undef (invalid)\n\nChecks if the variant part of a prefix only has 0s, and the length is correct.\n\n\"ipcheckprefix ($ip,$len,$ipv) or do {};\"\n\nipreverse\nGet a reverse name from a prefix\n\nParams  : IP, length of prefix, IP version\nReturns : Reverse name or undef (error)\n\n\"$reverse = ipreverse ($ip);\"\n\nipnormalize\nNormalize data to a range/prefix of IP addresses\n\nParams  : Data String (Single IP, Range, Prefix)\nReturns : ip1, ip2 (if range/prefix) or undef (error)\n\n\"($ip1,$ip2) = ipnormalize ($data);\"\n\nipauth\nReturn IP authority information from the IP::Authority module\n\nParams  : IP, version\nReturns : Auth info (RI for RIPE, AR for ARIN, etc)\n\n\"$auth = ipauth ($ip,4);\"\n\nNote: IPv4 only\n"
                }
            ]
        },
        "BUGS": {
            "content": "The Math::BigInt library is needed for functions that use integers. These are ipinttobin,\nipbintoint, and the size method. In a next version, Math::BigInt will become optional.\n",
            "subsections": []
        },
        "AUTHORS": {
            "content": "Manuel Valente <manuel.valente@gmail.com>.\n\nOriginal IPv4 code by Monica Cortes Sack <mcortes@ripe.net>.\n\nOriginal IPv6 code by Lee Wilmot <lee@ripe.net>.\n",
            "subsections": []
        },
        "BASED ON": {
            "content": "ipv4pack.pm, iplib.pm, iplibncc.pm.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "",
            "subsections": [
                {
                    "name": "perl",
                    "content": ""
                }
            ]
        }
    },
    "summary": "Net::IP - Perl extension for manipulating IPv4/IPv6 addresses",
    "flags": [],
    "examples": [],
    "see_also": []
}