{
    "mode": "perldoc",
    "parameter": "Net::CIDR",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Net%3A%3ACIDR/json",
    "generated": "2026-05-30T06:05:14Z",
    "synopsis": "use Net::CIDR;\nuse Net::CIDR ':all';\nmy $var;\nif ($var = Net::CIDR::cidrvalidate($var))\n{\n// ... do something\n}\nprint join(\"\\n\",\nNet::CIDR::range2cidr(\"192.168.0.0-192.168.255.255\",\n\"10.0.0.0-10.3.255.255\"))\n. \"\\n\";\n#\n# Output from above:\n#\n# 192.168.0.0/16\n# 10.0.0.0/14\nprint join(\"\\n\",\nNet::CIDR::range2cidr(\n\"dead:beef::-dead:beef:ffff:ffff:ffff:ffff:ffff:ffff\"))\n. \"\\n\";\n#\n# Output from above:\n#\n# dead:beef::/32\nprint join(\"\\n\",\nNet::CIDR::range2cidr(\"192.168.1.0-192.168.2.255\"))\n. \"\\n\";\n#\n# Output from above:\n#\n# 192.168.1.0/24\n# 192.168.2.0/24\nprint join(\"\\n\", Net::CIDR::cidr2range(\"192.168.0.0/16\")) . \"\\n\";\n#\n# Output from above:\n#\n# 192.168.0.0-192.168.255.255\nprint join(\"\\n\", Net::CIDR::cidr2range(\"dead::beef::/46\")) . \"\\n\";\n#\n# Output from above:\n#\n# dead:beef::-dead:beef:3:ffff:ffff:ffff:ffff:ffff\n@list=(\"192.168.0.0/24\");\n@list=Net::CIDR::cidradd(\"192.168.1.0-192.168.1.255\", @list);\nprint join(\"\\n\", @list) . \"\\n\";\n#\n# Output from above:\n#\n# 192.168.0.0/23\nprint join(\"\\n\", Net::CIDR::cidr2octets(\"192.168.0.0/22\")) . \"\\n\";\n#\n# Output from above:\n#\n# 192.168.0\n# 192.168.1\n# 192.168.2\n# 192.168.3\nprint join(\"\\n\", Net::CIDR::cidr2octets(\"dead::beef::/46\")) . \"\\n\";\n#\n# Output from above:\n#\n# dead:beef:0000\n# dead:beef:0001\n# dead:beef:0002\n# dead:beef:0003\n@list=(\"192.168.0.0/24\");\nprint Net::CIDR::cidrlookup(\"192.168.0.12\", @list);\n#\n# Output from above:\n#\n# 1\n@list = Net::CIDR::addr2cidr(\"192.168.0.31\");\nprint join(\"\\n\", @list);\n#\n# Output from above:\n#\n# 192.168.0.31/32\n# 192.168.0.30/31\n# 192.168.0.28/30\n# 192.168.0.24/29\n# 192.168.0.16/28\n# 192.168.0.0/27\n# 192.168.0.0/26\n# 192.168.0.0/25\n# 192.168.0.0/24\n# 192.168.0.0/23\n# [and so on]\nprint Net::CIDR::addrandmask2cidr(\"195.149.50.61\", \"255.255.255.248\").\"\\n\";\n#\n# Output from above:\n#\n# 195.149.50.56/29",
    "sections": {
        "NAME": {
            "content": "Net::CIDR - Manipulate IPv4/IPv6 netblocks in CIDR notation\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use Net::CIDR;\n\nuse Net::CIDR ':all';\n\nmy $var;\n\nif ($var = Net::CIDR::cidrvalidate($var))\n{\n// ... do something\n}\n\nprint join(\"\\n\",\nNet::CIDR::range2cidr(\"192.168.0.0-192.168.255.255\",\n\"10.0.0.0-10.3.255.255\"))\n. \"\\n\";\n#\n# Output from above:\n#\n# 192.168.0.0/16\n# 10.0.0.0/14\n\nprint join(\"\\n\",\nNet::CIDR::range2cidr(\n\"dead:beef::-dead:beef:ffff:ffff:ffff:ffff:ffff:ffff\"))\n. \"\\n\";\n\n#\n# Output from above:\n#\n# dead:beef::/32\n\nprint join(\"\\n\",\nNet::CIDR::range2cidr(\"192.168.1.0-192.168.2.255\"))\n. \"\\n\";\n#\n# Output from above:\n#\n# 192.168.1.0/24\n# 192.168.2.0/24\n\nprint join(\"\\n\", Net::CIDR::cidr2range(\"192.168.0.0/16\")) . \"\\n\";\n#\n# Output from above:\n#\n# 192.168.0.0-192.168.255.255\n\nprint join(\"\\n\", Net::CIDR::cidr2range(\"dead::beef::/46\")) . \"\\n\";\n#\n# Output from above:\n#\n# dead:beef::-dead:beef:3:ffff:ffff:ffff:ffff:ffff\n\n@list=(\"192.168.0.0/24\");\n@list=Net::CIDR::cidradd(\"192.168.1.0-192.168.1.255\", @list);\n\nprint join(\"\\n\", @list) . \"\\n\";\n#\n# Output from above:\n#\n# 192.168.0.0/23\n\nprint join(\"\\n\", Net::CIDR::cidr2octets(\"192.168.0.0/22\")) . \"\\n\";\n#\n# Output from above:\n#\n# 192.168.0\n# 192.168.1\n# 192.168.2\n# 192.168.3\n\nprint join(\"\\n\", Net::CIDR::cidr2octets(\"dead::beef::/46\")) . \"\\n\";\n#\n# Output from above:\n#\n# dead:beef:0000\n# dead:beef:0001\n# dead:beef:0002\n# dead:beef:0003\n\n@list=(\"192.168.0.0/24\");\nprint Net::CIDR::cidrlookup(\"192.168.0.12\", @list);\n#\n# Output from above:\n#\n# 1\n\n@list = Net::CIDR::addr2cidr(\"192.168.0.31\");\nprint join(\"\\n\", @list);\n#\n# Output from above:\n#\n# 192.168.0.31/32\n# 192.168.0.30/31\n# 192.168.0.28/30\n# 192.168.0.24/29\n# 192.168.0.16/28\n# 192.168.0.0/27\n# 192.168.0.0/26\n# 192.168.0.0/25\n# 192.168.0.0/24\n# 192.168.0.0/23\n# [and so on]\n\nprint Net::CIDR::addrandmask2cidr(\"195.149.50.61\", \"255.255.255.248\").\"\\n\";\n#\n# Output from above:\n#\n# 195.149.50.56/29\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "The Net::CIDR package contains functions that manipulate lists of IP\nnetblocks expressed in CIDR notation. The Net::CIDR functions handle\nboth IPv4 and IPv6 addresses.\n\nThe cidrvalidate() function, described below, checks that its argument\nis a single, valid IP address or a CIDR. The remaining functions expect\nthat their parameters consist of validated IPs or CIDRs. See\ncidrvalidate() and BUGS, below, for more information.\n\n@cidrlist=Net::CIDR::range2cidr(@rangelist);\nEach element in the @rangelist is a string \"start-finish\", where\n\"start\" is the first IP address and \"finish\" is the last IP address.\nrange2cidr() converts each range into an equivalent CIDR netblock. It\nreturns a list of netblocks except in the case where it is given only\none parameter and is called in scalar context.\n\nFor example:\n\n@a=Net::CIDR::range2cidr(\"192.168.0.0-192.168.255.255\");\n\nThe result is a one-element array, with $a[0] being \"192.168.0.0/16\".\nrange2cidr() processes each \"start-finish\" element in @rangelist\nseparately. But if invoked like so:\n\n$a=Net::CIDR::range2cidr(\"192.168.0.0-192.168.255.255\");\n\nThe result is a scalar \"192.168.0.0/16\".\n\nWhere each element cannot be expressed as a single CIDR netblock\nrange2cidr() will generate as many CIDR netblocks as are necessary to\ncover the full range of IP addresses. Example:\n\n@a=Net::CIDR::range2cidr(\"192.168.1.0-192.168.2.255\");\n\nThe result is a two element array: (\"192.168.1.0/24\",\"192.168.2.0/24\");\n\n@a=Net::CIDR::range2cidr(\n\"d08c:43::-d08c:43:ffff:ffff:ffff:ffff:ffff:ffff\");\n\nThe result is an one element array: (\"d08c:43::/32\") that reflects this\nIPv6 netblock in CIDR notation.\n\nrange2cidr() does not merge adjacent or overlapping netblocks in\n@rangelist.\n\n@rangelist=Net::CIDR::cidr2range(@cidrlist);\nThe cidr2range() functions converts a netblock list in CIDR notation to\na list of \"start-finish\" IP address ranges:\n\n@a=Net::CIDR::cidr2range(\"10.0.0.0/14\", \"192.168.0.0/24\");\n\nThe result is a two-element array: (\"10.0.0.0-10.3.255.255\",\n\"192.168.0.0-192.168.0.255\").\n\n@a=Net::CIDR::cidr2range(\"d08c:43::/32\");\n\nThe result is a one-element array:\n(\"d08c:43::-d08c:43:ffff:ffff:ffff:ffff:ffff:ffff\").\n\ncidr2range() does not merge adjacent or overlapping netblocks in\n@cidrlist.\n\n@netblocklist = Net::CIDR::addr2cidr($address);\nThe addr2cidr function takes an IP address and returns a list of all the\nCIDR netblocks it might belong to:\n\n@a=Net::CIDR::addr2cidr('192.168.0.31');\n\nThe result is a thirtythree-element array: ('192.168.0.31/32',\n'192.168.0.30/31', '192.168.0.28/30', '192.168.0.24/29', [and so on])\nconsisting of all the possible subnets containing this address from\n0.0.0.0/0 to address/32.\n\nAny addresses supplied to addr2cidr after the first will be ignored. It\nworks similarly for IPv6 addresses, returning a list of one hundred and\ntwenty nine elements.\n\n$cidr=Net::CIDR::addrandmask2cidr($address, $netmask);\nThe addrandmask2cidr function takes an IP address and a netmask, and\nreturns the CIDR range whose size fits the netmask and which contains\nthe address. It is an error to supply one parameter in IPv4-ish format\nand the other in IPv6-ish format, and it is an error to supply a netmask\nwhich does not consist solely of 1 bits followed by 0 bits. For example,\n'255.255.248.192' is an invalid netmask, as is '255.255.255.32' because\nboth contain 0 bits in between 1 bits.\n\nTechnically speaking both of those *are* valid netmasks, but a) you'd\nhave to be insane to use them, and b) there's no corresponding CIDR\nrange.\n\n@octetlist=Net::CIDR::cidr2octets(@cidrlist);\ncidr2octets() takes @cidrlist and returns a list of leading octets\nrepresenting those netblocks. Example:\n\n@octetlist=Net::CIDR::cidr2octets(\"10.0.0.0/14\", \"192.168.0.0/24\");\n\nThe result is the following five-element array: (\"10.0\", \"10.1\", \"10.2\",\n\"10.3\", \"192.168.0\").\n\nFor IPv6 addresses, the hexadecimal words in the resulting list are\nzero-padded:\n\n@octetlist=Net::CIDR::cidr2octets(\"::dead:beef:0:0/110\");\n\nThe result is a four-element array:\n(\"0000:0000:0000:0000:dead:beef:0000\",\n\"0000:0000:0000:0000:dead:beef:0001\",\n\"0000:0000:0000:0000:dead:beef:0002\",\n\"0000:0000:0000:0000:dead:beef:0003\"). Prefixes of IPv6 CIDR blocks\nshould be even multiples of 16 bits, otherwise they can potentially\nexpand out to a 32,768-element array, each!\n\n@cidrlist=Net::CIDR::cidradd($block, @cidrlist);\nThe cidradd() functions allows a CIDR list to be built one CIDR netblock\nat a time, merging adjacent and overlapping ranges. $block is a single\nnetblock, expressed as either \"start-finish\", or \"address/prefix\".\nExample:\n\n@cidrlist=Net::CIDR::range2cidr(\"192.168.0.0-192.168.0.255\");\n@cidrlist=Net::CIDR::cidradd(\"10.0.0.0/8\", @cidrlist);\n@cidrlist=Net::CIDR::cidradd(\"192.168.1.0-192.168.1.255\", @cidrlist);\n\nThe result is a two-element array: (\"10.0.0.0/8\", \"192.168.0.0/23\").\nIPv6 addresses are handled in an analogous fashion.\n\n$found=Net::CIDR::cidrlookup($ip, @cidrlist);\nSearch for $ip in @cidrlist. $ip can be a single IP address, or a\nnetblock in CIDR or start-finish notation. lookup() returns 1 if $ip\noverlaps any netblock in @cidrlist, 0 if not.\n\n$ip=Net::CIDR::cidrvalidate($ip);\nValidate whether $ip is a valid IPv4 or IPv6 address, or a CIDR. Returns\nits argument or undef. Spaces are removed, and IPv6 hexadecimal address\nare converted to lowercase.\n\n$ip with less than four octets gets filled out with additional octets,\nand the modified value gets returned. This turns \"192.168/16\" into a\nproper \"192.168.0.0/16\".\n\nIf $ip contains a \"/\", it must be a valid CIDR, otherwise it must be a\nvalid IPv4 or an IPv6 address.\n\nA technically invalid CIDR, such as \"192.168.0.1/24\" fails validation,\nreturning undef.\n",
            "subsections": []
        },
        "BUGS": {
            "content": "Garbage in, garbage out. Always use cidrvalidate() before doing anything\nwith untrusted input. Otherwise, \"slightly\" invalid input will work\n(extraneous whitespace is generally OK), but the functions will croak if\nyou're totally off the wall.\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Sam Varshavchik <sam@email-scan.com>\n\nWith some contributions from David Cantrell <david@cantrell.org.uk>\n",
            "subsections": []
        }
    },
    "summary": "Net::CIDR - Manipulate IPv4/IPv6 netblocks in CIDR notation",
    "flags": [],
    "examples": [],
    "see_also": []
}