{
    "mode": "perldoc",
    "parameter": "Net::netent",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Net%3A%3Anetent/json",
    "generated": "2026-06-11T06:14:13Z",
    "synopsis": "use Net::netent qw(:FIELDS);\ngetnetbyname(\"loopback\")               or die \"bad net\";\nprintf \"%s is %08X\\n\", $nname, $nnet;\nuse Net::netent;\n$n = getnetbyname(\"loopback\")          or die \"bad net\";\n{ # there's gotta be a better way, eh?\n@bytes = unpack(\"C4\", pack(\"N\", $n->net));\nshift @bytes while @bytes && $bytes[0] == 0;\n}\nprintf \"%s is %08X [%d.%d.%d.%d]\\n\", $n->name, $n->net, @bytes;",
    "sections": {
        "NAME": {
            "content": "Net::netent - by-name interface to Perl's built-in getnet*() functions\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use Net::netent qw(:FIELDS);\ngetnetbyname(\"loopback\")               or die \"bad net\";\nprintf \"%s is %08X\\n\", $nname, $nnet;\n\nuse Net::netent;\n\n$n = getnetbyname(\"loopback\")          or die \"bad net\";\n{ # there's gotta be a better way, eh?\n@bytes = unpack(\"C4\", pack(\"N\", $n->net));\nshift @bytes while @bytes && $bytes[0] == 0;\n}\nprintf \"%s is %08X [%d.%d.%d.%d]\\n\", $n->name, $n->net, @bytes;\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "This module's default exports override the core getnetbyname() and getnetbyaddr() functions,\nreplacing them with versions that return \"Net::netent\" objects. This object has methods that\nreturn the similarly named structure field name from the C's netent structure from netdb.h;\nnamely name, aliases, addrtype, and net. The aliases method returns an array reference, the rest\nscalars.\n\nYou may also import all the structure fields directly into your namespace as regular variables\nusing the :FIELDS import tag. (Note that this still overrides your core functions.) Access these\nfields as variables named with a preceding \"n\". Thus, \"$netobj->name()\" corresponds to $nname\nif you import the fields. Array references are available as regular array variables, so for\nexample \"@{ $netobj->aliases() }\" would be simply @naliases.\n\nThe getnet() function is a simple front-end that forwards a numeric argument to getnetbyaddr(),\nand the rest to getnetbyname().\n\nTo access this functionality without the core overrides, pass the \"use\" an empty import list,\nand then access function functions with their full qualified names. On the other hand, the\nbuilt-ins are still available via the \"CORE::\" pseudo-package.\n",
            "subsections": []
        },
        "EXAMPLES": {
            "content": "The getnet() functions do this in the Perl core:\n\nsvsetiv(sv, (I32)nent->nnet);\n\nThe gethost() functions do this in the Perl core:\n\nsvsetpvn(sv, hent->haddr, len);\n\nThat means that the address comes back in binary for the host functions, and as a regular perl\ninteger for the net ones. This seems a bug, but here's how to deal with it:\n\nuse strict;\nuse Socket;\nuse Net::netent;\n\n@ARGV = ('loopback') unless @ARGV;\n\nmy($n, $net);\n\nfor $net ( @ARGV ) {\n\nunless ($n = getnetbyname($net)) {\nwarn \"$0: no such net: $net\\n\";\nnext;\n}\n\nprintf \"\\n%s is %s%s\\n\",\n$net,\nlc($n->name) eq lc($net) ? \"\" : \"*really* \",\n$n->name;\n\nprint \"\\taliases are \", join(\", \", @{$n->aliases}), \"\\n\"\nif @{$n->aliases};\n\n# this is stupid; first, why is this not in binary?\n# second, why am i going through these convolutions\n# to make it looks right\n{\nmy @a = unpack(\"C4\", pack(\"N\", $n->net));\nshift @a while @a && $a[0] == 0;\nprintf \"\\taddr is %s [%d.%d.%d.%d]\\n\", $n->net, @a;\n}\n\nif ($n = getnetbyaddr($n->net)) {\nif (lc($n->name) ne lc($net)) {\nprintf \"\\tThat addr reverses to net %s!\\n\", $n->name;\n$net = $n->name;\nredo;\n}\n}\n}\n",
            "subsections": []
        },
        "NOTE": {
            "content": "While this class is currently implemented using the Class::Struct module to build a struct-like\nclass, you shouldn't rely upon this.\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Tom Christiansen\n",
            "subsections": []
        }
    },
    "summary": "Net::netent - by-name interface to Perl's built-in getnet*() functions",
    "flags": [],
    "examples": [
        "The getnet() functions do this in the Perl core:",
        "svsetiv(sv, (I32)nent->nnet);",
        "The gethost() functions do this in the Perl core:",
        "svsetpvn(sv, hent->haddr, len);",
        "That means that the address comes back in binary for the host functions, and as a regular perl",
        "integer for the net ones. This seems a bug, but here's how to deal with it:",
        "use strict;",
        "use Socket;",
        "use Net::netent;",
        "@ARGV = ('loopback') unless @ARGV;",
        "my($n, $net);",
        "for $net ( @ARGV ) {",
        "unless ($n = getnetbyname($net)) {",
        "warn \"$0: no such net: $net\\n\";",
        "next;",
        "printf \"\\n%s is %s%s\\n\",",
        "$net,",
        "lc($n->name) eq lc($net) ? \"\" : \"*really* \",",
        "$n->name;",
        "print \"\\taliases are \", join(\", \", @{$n->aliases}), \"\\n\"",
        "if @{$n->aliases};",
        "# this is stupid; first, why is this not in binary?",
        "# second, why am i going through these convolutions",
        "# to make it looks right",
        "my @a = unpack(\"C4\", pack(\"N\", $n->net));",
        "shift @a while @a && $a[0] == 0;",
        "printf \"\\taddr is %s [%d.%d.%d.%d]\\n\", $n->net, @a;",
        "if ($n = getnetbyaddr($n->net)) {",
        "if (lc($n->name) ne lc($net)) {",
        "printf \"\\tThat addr reverses to net %s!\\n\", $n->name;",
        "$net = $n->name;",
        "redo;"
    ],
    "see_also": []
}