1package Net::servent; 2use strict; 3 4use 5.006_001; 5our $VERSION = '1.02'; 6our(@EXPORT, @EXPORT_OK, %EXPORT_TAGS); 7our ( $s_name, @s_aliases, $s_port, $s_proto ); 8BEGIN { 9 use Exporter (); 10 @EXPORT = qw(getservbyname getservbyport getservent getserv); 11 @EXPORT_OK = qw( $s_name @s_aliases $s_port $s_proto ); 12 %EXPORT_TAGS = ( FIELDS => [ @EXPORT_OK, @EXPORT ] ); 13} 14 15# Class::Struct forbids use of @ISA 16sub import { goto &Exporter::import } 17 18use Class::Struct qw(struct); 19struct 'Net::servent' => [ 20 name => '$', 21 aliases => '@', 22 port => '$', 23 proto => '$', 24]; 25 26sub populate (@) { 27 return unless @_; 28 my $sob = new(); 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]; 33 return $sob; 34} 35 36sub getservent ( ) { populate(CORE::getservent()) } 37sub getservbyname ($;$) { populate(CORE::getservbyname(shift,shift||'tcp')) } 38sub getservbyport ($;$) { populate(CORE::getservbyport(shift,shift||'tcp')) } 39 40sub getserv ($;$) { 41 no strict 'refs'; 42 return &{'getservby' . ($_[0]=~/^\d+$/ ? 'port' : 'name')}(@_); 43} 44 451; 46 47__END__ 48 49=head1 NAME 50 51Net::servent - by-name interface to Perl's built-in getserv*() functions 52 53=head1 SYNOPSIS 54 55 use Net::servent; 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}"; 59 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"; 63 64=head1 DESCRIPTION 65 66This module's default exports override the core getservent(), 67getservbyname(), and 68getnetbyport() 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 70named structure field name from the C's servent structure from F<netdb.h>; 71namely name, aliases, port, and proto. The aliases 72method returns an array reference, the rest scalars. 73 74You may also import all the structure fields directly into your namespace 75as regular variables using the :FIELDS import tag. (Note that this still 76overrides your core functions.) Access these fields as variables named 77with a preceding C<s_>. Thus, C<$serv_obj-E<gt>name()> corresponds to 78$s_name if you import the fields. Array references are available as 79regular array variables, so for example C<@{ $serv_obj-E<gt>aliases()}> 80would be simply @s_aliases. 81 82The getserv() function is a simple front-end that forwards a numeric 83argument to getservbyport(), and the rest to getservbyname(). 84 85To access this functionality without the core overrides, 86pass the C<use> an empty import list, and then access 87function functions with their full qualified names. 88On the other hand, the built-ins are still available 89via the C<CORE::> pseudo-package. 90 91=head1 EXAMPLES 92 93 use Net::servent qw(:FIELDS); 94 95 while (@ARGV) { 96 my ($service, $proto) = ((split m!/!, shift), 'tcp'); 97 my $valet = getserv($service, $proto); 98 unless ($valet) { 99 warn "$0: No service: $service/$proto\n" 100 next; 101 } 102 printf "service $service/$proto is port %d\n", $valet->port; 103 print "alias are @s_aliases\n" if @s_aliases; 104 } 105 106=head1 NOTE 107 108While this class is currently implemented using the Class::Struct 109module to build a struct-like class, you shouldn't rely upon this. 110 111=head1 AUTHOR 112 113Tom Christiansen 114