--- name: getnetent signature: 'getnetent' since: 5.0 status: documented categories: ["Network info"] --- ```{index} single: getnetent; Perl built-in ``` *[Network info](../perlfunc-by-category)* # getnetent Read the next entry from the networks database. `getnetent` wraps the C library routine of the same name. It walks the system networks database (usually `/etc/networks`, sometimes augmented by NIS or other resolver backends) one record at a time, returning the next entry on each call. Pair it with [`setnetent`](setnetent) to rewind or control the connection, and [`endnetent`](endnetent) to close it. ## Synopsis ```perl my ($name, $aliases, $addrtype, $net) = getnetent; my $name = getnetent; # scalar context ``` ## What you get back In list context, a four-element list: - `$name` — canonical network name (e.g. `"loopback"`). - `$aliases` — space-separated string of alternative names. - `$addrtype` — address family, typically `AF_INET` (`2`). - `$net` — network number as an integer in host byte order. Convert to dotted form with `Socket::inet_ntoa(pack("N", $net))`. In scalar context, just `$name`. When the iterator is exhausted (no more records), list context returns the empty list and scalar context returns [`undef`](undef). A missing entry during a direct lookup returns a single meaningless true value per perlfunc convention, but since `getnetent` takes no key, the only terminal condition is "end of database." ## Global state it touches `getnetent` reads from a **process-wide iterator** maintained by the C library. Every call advances that iterator; there is no per-scope state, and concurrent iteration from two places in the same process will interleave records. [`setnetent`](setnetent) rewinds it and optionally asks the library to keep the backing connection open for the duration of iteration; [`endnetent`](endnetent) closes it. ## Examples Walk the entire networks database: ```perl setnetent(1); while (my ($name, $aliases, $addrtype, $net) = getnetent) { printf "%-20s %s\n", $name, join(" ", split / /, $aliases); } endnetent(); ``` Scalar context gives just the name — useful for a quick enumeration: ```perl setnetent(1); while (defined(my $name = getnetent)) { print "$name\n"; } endnetent(); ``` Decode the network number into dotted form: ```perl use Socket; setnetent(1); while (my ($name, undef, undef, $net) = getnetent) { printf "%-20s %s\n", $name, inet_ntoa(pack "N", $net); } endnetent(); ``` Object-oriented access through `Net::netent` — the by-name interface from the standard library replaces the positional return list with a blessed object: ```perl use Net::netent; while (my $n = getnetent()) { printf "%s => %d\n", $n->name, $n->net; } ``` ## Edge cases - **Iterator exhaustion**: list context returns `()`, scalar context returns [`undef`](undef). The loop forms above terminate naturally. - **No `setnetent` before the loop**: the first call implicitly opens the database. Calling `setnetent(1)` is still recommended — without the "stay open" flag, some C libraries reopen the file on every record and performance collapses. - **Multiple concurrent walks**: the iterator is process-global. Two loops over `getnetent` in the same process will split the records between them. Collect into an array first if both need the full set. - **Unsupported on some platforms**: Windows has no `/etc/networks` equivalent and the underlying C routine is absent. Attempting to call `getnetent` there raises `The getnetent function is unimplemented`. On Linux, glibc provides the function but the database is often empty — most systems rely on DNS instead, which this interface does not consult. - **Thread safety**: the C library's iterator is not thread-safe. Perl may transparently substitute `getnetent_r` on systems that provide it, but do not assume this is universal. In a threaded program, serialise access through a single thread. - **Tainting**: when tainting is enabled, all four return fields are tainted — they come from a file (or a remote resolver) outside the program's control. ## Differences from upstream Fully compatible with upstream Perl 5.42. ## See also - [`setnetent`](setnetent) — rewind the iterator and optionally keep the backing connection open across calls - [`endnetent`](endnetent) — close the iterator when you're done - [`getnetbyname`](getnetbyname) — look up one network by name instead of walking the whole database - [`getnetbyaddr`](getnetbyaddr) — look up one network by its address - [`gethostent`](gethostent) — the analogous walk over the hosts database, with the same `set`/`end` companion pair - `Net::netent` — by-name object interface that replaces the positional return list