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