1Net::servent(3pm) Perl Programmers Reference Guide Net::servent(3pm)
2
3
4
6 Net::servent - by-name interface to Perl's built-in getserv*()
7 functions
8
10 use Net::servent;
11 my $s = getservbyname(shift || 'ftp') || die "no service";
12 printf "port for %s is %s, aliases are %s\n",
13 $s->name, $s->port, "@{$s->aliases}";
14
15 use Net::servent qw(:FIELDS);
16 getservbyname(shift || 'ftp') || die "no service";
17 print "port for $s_name is $s_port, aliases are @s_aliases\n";
18
20 This module's default exports override the core getservent(),
21 getservbyname(), and getnetbyport() functions, replacing them with
22 versions that return "Net::servent" objects. They take default second
23 arguments of "tcp". This object has methods that return the similarly
24 named structure field name from the C's servent structure from netdb.h;
25 namely name, aliases, port, and proto. The aliases method returns an
26 array reference, the rest scalars.
27
28 You may also import all the structure fields directly into your
29 namespace as regular variables using the :FIELDS import tag. (Note
30 that this still overrides your core functions.) Access these fields as
31 variables named with a preceding "s_". Thus, "$serv_obj->name()"
32 corresponds to $s_name if you import the fields. Array references are
33 available as regular array variables, so for example "@{
34 $serv_obj->aliases()}" would be simply @s_aliases.
35
36 The getserv() function is a simple front-end that forwards a numeric
37 argument to getservbyport(), and the rest to getservbyname().
38
39 To access this functionality without the core overrides, pass the "use"
40 an empty import list, and then access function functions with their
41 full qualified names. On the other hand, the built-ins are still
42 available via the "CORE::" pseudo-package.
43
45 use Net::servent qw(:FIELDS);
46
47 while (@ARGV) {
48 my ($service, $proto) = ((split m!/!, shift), 'tcp');
49 my $valet = getserv($service, $proto);
50 unless ($valet) {
51 warn "$0: No service: $service/$proto\n"
52 next;
53 }
54 printf "service $service/$proto is port %d\n", $valet->port;
55 print "alias are @s_aliases\n" if @s_aliases;
56 }
57
59 While this class is currently implemented using the Class::Struct
60 module to build a struct-like class, you shouldn't rely upon this.
61
63 Tom Christiansen
64
65
66
67perl v5.38.2 2023-11-30 Net::servent(3pm)