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