6 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
8 @EXPORT = qw(getservbyname getservbyport getservent getserv);
9 @EXPORT_OK = qw( $s_name @s_aliases $s_port $s_proto );
10 %EXPORT_TAGS = ( FIELDS => [ @EXPORT_OK, @EXPORT ] );
14 use Class::Template qw(struct);
15 struct 'Net::servent' => [
25 $s_name = $sob->[0] = $_[0];
26 @s_aliases = @{ $sob->[1] } = split ' ', $_[1];
27 $s_port = $sob->[2] = $_[2];
28 $s_proto = $sob->[3] = $_[3];
32 sub getservent ( ) { populate(CORE::getservent()) }
33 sub getservbyname ($;$) { populate(CORE::getservbyname(shift,shift||'tcp')) }
34 sub getservbyport ($;$) { populate(CORE::getservbyport(shift,shift||'tcp')) }
38 return &{'getservby' . ($_[0]=~/^\d+$/ ? 'port' : 'name')}(@_);
47 Net::servent - by-name interface to Perl's built-in getserv*() functions
52 $s = getservbyname(shift || 'ftp') || die "no service";
53 printf "port for %s is %s, aliases are %s\n",
54 $s->name, $s->port, "@{$s->aliases}";
56 use Net::servent qw(:FIELDS);
57 getservbyname(shift || 'ftp') || die "no service";
58 print "port for $s_name is $s_port, aliases are @s_aliases\n";
62 This module's default exports override the core getservent(),
64 getnetbyport() functions, replacing them with versions that return
65 "Net::servent" objects. They take default second arguments of "tcp". This object has methods that return the similarly
66 named structure field name from the C's servent structure from F<netdb.h>;
67 namely name, aliases, port, and proto. The aliases
68 method returns an array reference, the rest scalars.
70 You may also import all the structure fields directly into your namespace
71 as regular variables using the :FIELDS import tag. (Note that this still
72 overrides your core functions.) Access these fields as variables named
73 with a preceding C<n_>. Thus, C<$serv_obj-E<gt>name()> corresponds to
74 $s_name if you import the fields. Array references are available as
75 regular array variables, so for example C<@{ $serv_obj-E<gt>aliases()
76 }> would be simply @s_aliases.
78 The getserv() function is a simple front-end that forwards a numeric
79 argument to getservbyport(), and the rest to getservbyname().
81 To access this functionality without the core overrides,
82 pass the C<use> an empty import list, and then access
83 function functions with their full qualified names.
84 On the other hand, the built-ins are still available
85 via the C<CORE::> pseudo-package.
89 use Net::servent qw(:FIELDS);
92 my ($service, $proto) = ((split m!/!, shift), 'tcp');
93 my $valet = getserv($service, $proto);
95 warn "$0: No service: $service/$proto\n"
98 printf "service $service/$proto is port %d\n", $valet->port;
99 print "alias are @s_aliases\n" if @s_aliases;
104 While this class is currently implemented using the Class::Template
105 module to build a struct-like class, you shouldn't rely upon this.