xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/lib/Net/protoent.pm (revision 0:68f95e015346)
1*0Sstevel@tonic-gatepackage Net::protoent;
2*0Sstevel@tonic-gateuse strict;
3*0Sstevel@tonic-gate
4*0Sstevel@tonic-gateuse 5.006_001;
5*0Sstevel@tonic-gateour $VERSION = '1.00';
6*0Sstevel@tonic-gateour(@EXPORT, @EXPORT_OK, %EXPORT_TAGS);
7*0Sstevel@tonic-gateBEGIN {
8*0Sstevel@tonic-gate    use Exporter   ();
9*0Sstevel@tonic-gate    @EXPORT      = qw(getprotobyname getprotobynumber getprotoent getproto);
10*0Sstevel@tonic-gate    @EXPORT_OK   = qw( $p_name @p_aliases $p_proto );
11*0Sstevel@tonic-gate    %EXPORT_TAGS = ( FIELDS => [ @EXPORT_OK, @EXPORT ] );
12*0Sstevel@tonic-gate}
13*0Sstevel@tonic-gateuse vars      @EXPORT_OK;
14*0Sstevel@tonic-gate
15*0Sstevel@tonic-gate# Class::Struct forbids use of @ISA
16*0Sstevel@tonic-gatesub import { goto &Exporter::import }
17*0Sstevel@tonic-gate
18*0Sstevel@tonic-gateuse Class::Struct qw(struct);
19*0Sstevel@tonic-gatestruct 'Net::protoent' => [
20*0Sstevel@tonic-gate   name		=> '$',
21*0Sstevel@tonic-gate   aliases	=> '@',
22*0Sstevel@tonic-gate   proto	=> '$',
23*0Sstevel@tonic-gate];
24*0Sstevel@tonic-gate
25*0Sstevel@tonic-gatesub populate (@) {
26*0Sstevel@tonic-gate    return unless @_;
27*0Sstevel@tonic-gate    my $pob = new();
28*0Sstevel@tonic-gate    $p_name 	 =    $pob->[0]     	     = $_[0];
29*0Sstevel@tonic-gate    @p_aliases	 = @{ $pob->[1] } = split ' ', $_[1];
30*0Sstevel@tonic-gate    $p_proto	 =    $pob->[2] 	     = $_[2];
31*0Sstevel@tonic-gate    return $pob;
32*0Sstevel@tonic-gate}
33*0Sstevel@tonic-gate
34*0Sstevel@tonic-gatesub getprotoent      ( )  { populate(CORE::getprotoent()) }
35*0Sstevel@tonic-gatesub getprotobyname   ($)  { populate(CORE::getprotobyname(shift)) }
36*0Sstevel@tonic-gatesub getprotobynumber ($)  { populate(CORE::getprotobynumber(shift)) }
37*0Sstevel@tonic-gate
38*0Sstevel@tonic-gatesub getproto ($;$) {
39*0Sstevel@tonic-gate    no strict 'refs';
40*0Sstevel@tonic-gate    return &{'getprotoby' . ($_[0]=~/^\d+$/ ? 'number' : 'name')}(@_);
41*0Sstevel@tonic-gate}
42*0Sstevel@tonic-gate
43*0Sstevel@tonic-gate1;
44*0Sstevel@tonic-gate
45*0Sstevel@tonic-gate__END__
46*0Sstevel@tonic-gate
47*0Sstevel@tonic-gate=head1 NAME
48*0Sstevel@tonic-gate
49*0Sstevel@tonic-gateNet::protoent - by-name interface to Perl's built-in getproto*() functions
50*0Sstevel@tonic-gate
51*0Sstevel@tonic-gate=head1 SYNOPSIS
52*0Sstevel@tonic-gate
53*0Sstevel@tonic-gate use Net::protoent;
54*0Sstevel@tonic-gate $p = getprotobyname(shift || 'tcp') || die "no proto";
55*0Sstevel@tonic-gate printf "proto for %s is %d, aliases are %s\n",
56*0Sstevel@tonic-gate    $p->name, $p->proto, "@{$p->aliases}";
57*0Sstevel@tonic-gate
58*0Sstevel@tonic-gate use Net::protoent qw(:FIELDS);
59*0Sstevel@tonic-gate getprotobyname(shift || 'tcp') || die "no proto";
60*0Sstevel@tonic-gate print "proto for $p_name is $p_proto, aliases are @p_aliases\n";
61*0Sstevel@tonic-gate
62*0Sstevel@tonic-gate=head1 DESCRIPTION
63*0Sstevel@tonic-gate
64*0Sstevel@tonic-gateThis module's default exports override the core getprotoent(),
65*0Sstevel@tonic-gategetprotobyname(), and getnetbyport() functions, replacing them with
66*0Sstevel@tonic-gateversions that return "Net::protoent" objects.  They take default
67*0Sstevel@tonic-gatesecond arguments of "tcp".  This object has methods that return the
68*0Sstevel@tonic-gatesimilarly named structure field name from the C's protoent structure
69*0Sstevel@tonic-gatefrom F<netdb.h>; namely name, aliases, and proto.  The aliases method
70*0Sstevel@tonic-gatereturns an array reference, the rest scalars.
71*0Sstevel@tonic-gate
72*0Sstevel@tonic-gateYou may also import all the structure fields directly into your namespace
73*0Sstevel@tonic-gateas regular variables using the :FIELDS import tag.  (Note that this still
74*0Sstevel@tonic-gateoverrides your core functions.)  Access these fields as variables named
75*0Sstevel@tonic-gatewith a preceding C<p_>.  Thus, C<$proto_obj-E<gt>name()> corresponds to
76*0Sstevel@tonic-gate$p_name if you import the fields.  Array references are available as
77*0Sstevel@tonic-gateregular array variables, so for example C<@{ $proto_obj-E<gt>aliases()
78*0Sstevel@tonic-gate}> would be simply @p_aliases.
79*0Sstevel@tonic-gate
80*0Sstevel@tonic-gateThe getproto() function is a simple front-end that forwards a numeric
81*0Sstevel@tonic-gateargument to getprotobyport(), and the rest to getprotobyname().
82*0Sstevel@tonic-gate
83*0Sstevel@tonic-gateTo access this functionality without the core overrides,
84*0Sstevel@tonic-gatepass the C<use> an empty import list, and then access
85*0Sstevel@tonic-gatefunction functions with their full qualified names.
86*0Sstevel@tonic-gateOn the other hand, the built-ins are still available
87*0Sstevel@tonic-gatevia the C<CORE::> pseudo-package.
88*0Sstevel@tonic-gate
89*0Sstevel@tonic-gate=head1 NOTE
90*0Sstevel@tonic-gate
91*0Sstevel@tonic-gateWhile this class is currently implemented using the Class::Struct
92*0Sstevel@tonic-gatemodule to build a struct-like class, you shouldn't rely upon this.
93*0Sstevel@tonic-gate
94*0Sstevel@tonic-gate=head1 AUTHOR
95*0Sstevel@tonic-gate
96*0Sstevel@tonic-gateTom Christiansen
97