1package Net::protoent; 2use strict; 3 4use 5.006_001; 5our $VERSION = '1.01'; 6our(@EXPORT, @EXPORT_OK, %EXPORT_TAGS); 7our ( $p_name, @p_aliases, $p_proto ); 8BEGIN { 9 use Exporter (); 10 @EXPORT = qw(getprotobyname getprotobynumber getprotoent getproto); 11 @EXPORT_OK = qw( $p_name @p_aliases $p_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::protoent' => [ 20 name => '$', 21 aliases => '@', 22 proto => '$', 23]; 24 25sub populate (@) { 26 return unless @_; 27 my $pob = new(); 28 $p_name = $pob->[0] = $_[0]; 29 @p_aliases = @{ $pob->[1] } = split ' ', $_[1]; 30 $p_proto = $pob->[2] = $_[2]; 31 return $pob; 32} 33 34sub getprotoent ( ) { populate(CORE::getprotoent()) } 35sub getprotobyname ($) { populate(CORE::getprotobyname(shift)) } 36sub getprotobynumber ($) { populate(CORE::getprotobynumber(shift)) } 37 38sub getproto ($;$) { 39 no strict 'refs'; 40 return &{'getprotoby' . ($_[0]=~/^\d+$/ ? 'number' : 'name')}(@_); 41} 42 431; 44 45__END__ 46 47=head1 NAME 48 49Net::protoent - by-name interface to Perl's built-in getproto*() functions 50 51=head1 SYNOPSIS 52 53 use Net::protoent; 54 $p = getprotobyname(shift || 'tcp') || die "no proto"; 55 printf "proto for %s is %d, aliases are %s\n", 56 $p->name, $p->proto, "@{$p->aliases}"; 57 58 use Net::protoent qw(:FIELDS); 59 getprotobyname(shift || 'tcp') || die "no proto"; 60 print "proto for $p_name is $p_proto, aliases are @p_aliases\n"; 61 62=head1 DESCRIPTION 63 64This module's default exports override the core getprotoent(), 65getprotobyname(), and getnetbyport() functions, replacing them with 66versions that return "Net::protoent" objects. They take default 67second arguments of "tcp". This object has methods that return the 68similarly named structure field name from the C's protoent structure 69from F<netdb.h>; namely name, aliases, and proto. The aliases method 70returns an array reference, the rest scalars. 71 72You may also import all the structure fields directly into your namespace 73as regular variables using the :FIELDS import tag. (Note that this still 74overrides your core functions.) Access these fields as variables named 75with a preceding C<p_>. Thus, C<$proto_obj-E<gt>name()> corresponds to 76$p_name if you import the fields. Array references are available as 77regular array variables, so for example C<@{ $proto_obj-E<gt>aliases() 78}> would be simply @p_aliases. 79 80The getproto() function is a simple front-end that forwards a numeric 81argument to getprotobyport(), and the rest to getprotobyname(). 82 83To access this functionality without the core overrides, 84pass the C<use> an empty import list, and then access 85function functions with their full qualified names. 86On the other hand, the built-ins are still available 87via the C<CORE::> pseudo-package. 88 89=head1 NOTE 90 91While this class is currently implemented using the Class::Struct 92module to build a struct-like class, you shouldn't rely upon this. 93 94=head1 AUTHOR 95 96Tom Christiansen 97