--- name: endservent signature: 'endservent' since: 5.0 status: documented categories: ["Network info"] --- ```{index} single: endservent; Perl built-in ``` *[Network info](../perlfunc-by-category)* # endservent Close the services database after a walk with [`getservent`](getservent). `endservent` is the teardown half of the services-database iteration triplet. After [`setservent`](setservent) opens the database (typically `/etc/services` on Linux) and a sequence of [`getservent`](getservent) calls walks it entry by entry, `endservent` releases the underlying handle and any buffered state the C library is holding. It wraps the POSIX `endservent(3)` call one-to-one. ## Synopsis ```perl endservent; endservent(); ``` ## What you get back Nothing useful. The function is called for its side effect; it does not return a meaningful value. Do not test the return value. ## Global state it touches - The system-wide services-database iterator opened by [`setservent`](setservent) or implicitly by the first [`getservent`](getservent) / [`getservbyname`](getservbyname) / [`getservbyport`](getservbyport) call. `endservent` closes that iterator. - [`$!`](../perlvar) is **not** meaningfully set by this call; the POSIX contract for `endservent(3)` defines no error return. ## Examples Paired with [`setservent`](setservent) and [`getservent`](getservent) to enumerate every entry in `/etc/services`: ```perl setservent 0; while (my @e = getservent) { last unless @e; my ($name, $aliases, $port, $proto) = @e; print "$name/$proto = $port\n"; } endservent; ``` Close a walk early. The database was opened implicitly by the first [`getservent`](getservent); `endservent` is still the right way to release it: ```perl while (my @e = getservent) { last if $e[0] eq 'http'; } endservent; ``` Defensive cleanup around a block that may leave the iterator open. Harmless when no iteration is in progress: ```perl eval { setservent 1; scan_services(); 1; } or do { endservent; # always release the handle die $@; }; endservent; ``` ## Edge cases - **Calling without a preceding [`setservent`](setservent) or [`getservent`](getservent)** is harmless. The underlying C call tolerates a no-op close. - **Calling twice** is harmless for the same reason. Repeated `endservent` does not report an error. - **Does not affect the name and port lookup functions** — [`getservbyname`](getservbyname) and [`getservbyport`](getservbyport) open and close their own short-lived handles and are unaffected by the iterator state that `endservent` manages. - **No prototype, no arguments**. `endservent $x` is a compile-time error. Use `endservent;` or `endservent();`. - **Not portable beyond Unix-like systems**. On platforms where the C library lacks `endservent(3)`, the call croaks with `endservent not implemented`. ## Differences from upstream Fully compatible with upstream Perl 5.42. ## See also - [`setservent`](setservent) — open the services database and, with a non-zero `STAYOPEN`, keep the handle across subsequent lookups - [`getservent`](getservent) — read the next entry from the open services database; pair every iteration loop with `endservent` - [`getservbyname`](getservbyname) — one-shot lookup by service name; does not interact with the iterator `endservent` closes - [`getservbyport`](getservbyport) — one-shot lookup by port number; same independence from the iterator - [`endprotoent`](endprotoent) — the protocols-database analogue, same shape and same rules - [`endhostent`](endhostent) — the hosts-database analogue