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