1N/Apackage Net::servent;
1N/Ause strict;
1N/A
1N/Ause 5.006_001;
1N/Aour $VERSION = '1.01';
1N/Aour(@EXPORT, @EXPORT_OK, %EXPORT_TAGS);
1N/ABEGIN {
1N/A use Exporter ();
1N/A @EXPORT = qw(getservbyname getservbyport getservent getserv);
1N/A @EXPORT_OK = qw( $s_name @s_aliases $s_port $s_proto );
1N/A %EXPORT_TAGS = ( FIELDS => [ @EXPORT_OK, @EXPORT ] );
1N/A}
1N/Ause vars @EXPORT_OK;
1N/A
1N/A# Class::Struct forbids use of @ISA
1N/Asub import { goto &Exporter::import }
1N/A
1N/Ause Class::Struct qw(struct);
1N/Astruct 'Net::servent' => [
1N/A name => '$',
1N/A aliases => '@',
1N/A port => '$',
1N/A proto => '$',
1N/A];
1N/A
1N/Asub populate (@) {
1N/A return unless @_;
1N/A my $sob = new();
1N/A $s_name = $sob->[0] = $_[0];
1N/A @s_aliases = @{ $sob->[1] } = split ' ', $_[1];
1N/A $s_port = $sob->[2] = $_[2];
1N/A $s_proto = $sob->[3] = $_[3];
1N/A return $sob;
1N/A}
1N/A
1N/Asub getservent ( ) { populate(CORE::getservent()) }
1N/Asub getservbyname ($;$) { populate(CORE::getservbyname(shift,shift||'tcp')) }
1N/Asub getservbyport ($;$) { populate(CORE::getservbyport(shift,shift||'tcp')) }
1N/A
1N/Asub getserv ($;$) {
1N/A no strict 'refs';
1N/A return &{'getservby' . ($_[0]=~/^\d+$/ ? 'port' : 'name')}(@_);
1N/A}
1N/A
1N/A1;
1N/A
1N/A__END__
1N/A
1N/A=head1 NAME
1N/A
1N/ANet::servent - by-name interface to Perl's built-in getserv*() functions
1N/A
1N/A=head1 SYNOPSIS
1N/A
1N/A use Net::servent;
1N/A $s = getservbyname(shift || 'ftp') || die "no service";
1N/A printf "port for %s is %s, aliases are %s\n",
1N/A $s->name, $s->port, "@{$s->aliases}";
1N/A
1N/A use Net::servent qw(:FIELDS);
1N/A getservbyname(shift || 'ftp') || die "no service";
1N/A print "port for $s_name is $s_port, aliases are @s_aliases\n";
1N/A
1N/A=head1 DESCRIPTION
1N/A
1N/AThis module's default exports override the core getservent(),
1N/Agetservbyname(), and
1N/Agetnetbyport() functions, replacing them with versions that return
1N/A"Net::servent" objects. They take default second arguments of "tcp". This object has methods that return the similarly
1N/Anamed structure field name from the C's servent structure from F<netdb.h>;
1N/Anamely name, aliases, port, and proto. The aliases
1N/Amethod returns an array reference, the rest scalars.
1N/A
1N/AYou may also import all the structure fields directly into your namespace
1N/Aas regular variables using the :FIELDS import tag. (Note that this still
1N/Aoverrides your core functions.) Access these fields as variables named
1N/Awith a preceding C<s_>. Thus, C<$serv_obj-E<gt>name()> corresponds to
1N/A$s_name if you import the fields. Array references are available as
1N/Aregular array variables, so for example C<@{ $serv_obj-E<gt>aliases()}>
1N/Awould be simply @s_aliases.
1N/A
1N/AThe getserv() function is a simple front-end that forwards a numeric
1N/Aargument to getservbyport(), and the rest to getservbyname().
1N/A
1N/ATo access this functionality without the core overrides,
1N/Apass the C<use> an empty import list, and then access
1N/Afunction functions with their full qualified names.
1N/AOn the other hand, the built-ins are still available
1N/Avia the C<CORE::> pseudo-package.
1N/A
1N/A=head1 EXAMPLES
1N/A
1N/A use Net::servent qw(:FIELDS);
1N/A
1N/A while (@ARGV) {
1N/A my ($service, $proto) = ((split m!/!, shift), 'tcp');
1N/A my $valet = getserv($service, $proto);
1N/A unless ($valet) {
1N/A warn "$0: No service: $service/$proto\n"
1N/A next;
1N/A }
1N/A printf "service $service/$proto is port %d\n", $valet->port;
1N/A print "alias are @s_aliases\n" if @s_aliases;
1N/A }
1N/A
1N/A=head1 NOTE
1N/A
1N/AWhile this class is currently implemented using the Class::Struct
1N/Amodule to build a struct-like class, you shouldn't rely upon this.
1N/A
1N/A=head1 AUTHOR
1N/A
1N/ATom Christiansen