endservent#
Close the services database after a walk with getservent.
endservent is the teardown half of the services-database iteration
triplet. After setservent opens the database (typically
/etc/services on Linux) and a sequence of 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#
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
setserventor implicitly by the firstgetservent/getservbyname/getservbyportcall.endserventcloses that iterator.$!is not meaningfully set by this call; the POSIX contract forendservent(3)defines no error return.
Examples#
Paired with setservent and getservent to
enumerate every entry in /etc/services:
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; endservent is still the right way to
release it:
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:
eval {
setservent 1;
scan_services();
1;
} or do {
endservent; # always release the handle
die $@;
};
endservent;
Edge cases#
Calling without a preceding
setserventorgetserventis harmless. The underlying C call tolerates a no-op close.Calling twice is harmless for the same reason. Repeated
endserventdoes not report an error.Does not affect the name and port lookup functions —
getservbynameandgetservbyportopen and close their own short-lived handles and are unaffected by the iterator state thatendserventmanages.No prototype, no arguments.
endservent $xis a compile-time error. Useendservent;orendservent();.Not portable beyond Unix-like systems. On platforms where the C library lacks
endservent(3), the call croaks withendservent not implemented.
Differences from upstream#
Fully compatible with upstream Perl 5.42.
See also#
setservent— open the services database and, with a non-zeroSTAYOPEN, keep the handle across subsequent lookupsgetservent— read the next entry from the open services database; pair every iteration loop withendserventgetservbyname— one-shot lookup by service name; does not interact with the iteratorendserventclosesgetservbyport— one-shot lookup by port number; same independence from the iteratorendprotoent— the protocols-database analogue, same shape and same rulesendhostent— the hosts-database analogue