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