Net::servent(3perl)

πŸ’– Net::servent β€” the by-name bestie for Perl's built-in getserv*() girlies πŸ’–

okay so this is a page from the Perl Programmers Reference Guide, section 3perl, and the module is literally called Net::servent. she's a whole vibe. let's get into it 🫢


✨ NAME ✨

Net::servent β€” a by-name interface to Perl's built-in getserv*() functions. yes bestie, that's the whole pitch, she's a wrapper and she's iconic for it πŸ’…


🌸 SYNOPSIS 🌸

use Net::servent;
my $s = getservbyname(shift || 'ftp') || die "no service";
printf "port for %s is %s, aliases are %s\n",
   $s->name, $s->port, "@{$s->aliases}";

use Net::servent qw(:FIELDS);
getservbyname(shift || 'ftp') || die "no service";
print "port for $s_name is $s_port, aliases are @s_aliases\n";

see that? two whole ways to live your life. object girlie or variable girlie. both valid. both slay ✨


πŸ¦‹ DESCRIPTION πŸ¦‹

so the tea is this: by default, this module's exports override the core getservent(), getservbyname(), and getnetbyport() functions, and swap them out for versions that hand you back Net::servent objects instead. and they take a default second argument of "tcp" β€” so if you're lazy (affectionate) about it, she's got you πŸ’–

this object has methods that return the similarly named structure field name from C's servent structure from netdb.h. the fields are, in order:

  • name
  • aliases
  • port
  • proto

and here's the detail that matters: the aliases method returns an array reference, and literally all the rest return scalars. don't forget that, it's giving gotcha 😭


πŸŽ€ the :FIELDS tag πŸŽ€

you can also import all the structure fields directly into your namespace as regular variables using the :FIELDS import tag. (heads up: this still overrides your core functions, no cap β€” it's not a get-out-of-jail-free card.)

access these fields as variables named with a preceding "s_". so like, "$serv_obj->name()" corresponds to $s_name if you import the fields. slay.

array references are available as regular array variables too, so for example "@{$serv_obj->aliases()}" would be simply @s_aliases. so much cleaner, we love to see it πŸ’…


🌈 getserv() 🌈

the getserv() function is a simple front-end that forwards a numeric argument to getservbyport(), and the rest to getservbyname(). that's it. that's her whole job. queen of delegation πŸ‘‘


πŸ’œ the no-override route πŸ’œ

what if you want this functionality but you don't want the core overrides? valid. pass the "use" an empty import list, and then access function functions with their full qualified names. (yes, it literally says "function functions." we keep the original wording, it's part of the lore.)

on the other hand, the built-ins are still available via the "CORE::" pseudo-package. so nothing is ever truly gone, bestie 🫢


πŸ”₯ EXAMPLES πŸ”₯

okay this one's serving main character energy fr fr:

use Net::servent qw(:FIELDS);

while (@ARGV) {
    my ($service, $proto) = ((split m!/!, shift), 'tcp');
    my $valet = getserv($service, $proto);
    unless ($valet) {
        warn "$0: No service: $service/$proto\n"
        next;
    }
    printf "service $service/$proto is port %d\n", $valet->port;
    print "alias are @s_aliases\n" if @s_aliases;
}

look at her go. splitting on !, defaulting to 'tcp', warning when it's a no, printing ports and aliases. chef's kiss 😭✨


πŸ’€ NOTE πŸ’€

while this class is currently implemented using the Class::Struct module to build a struct-like class, you shouldn't rely upon this. it's giving "implementation detail, don't @ me." so don't build your whole personality around it, bestie πŸ’€

πŸ‘‘ AUTHOR πŸ‘‘

Tom Christiansen


aaaaand that's the whole page!! 🌸

this was Net::servent, from perl v5.44.0, dated 2026-10-17, living in Net::servent(3perl) β€” and yes, the section name is right there in the header too, so you'll never lose her. no cap. periodt. go read your service table, bestie πŸ’–πŸ¦‹βœ¨

πŸ“¦ where this page comes from
  • arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms