[dummy merge]
[p5sagit/p5-mst-13.2.git] / lib / Net / protoent.pm
CommitLineData
36477c24 1package Net::protoent;
2use strict;
3
4BEGIN {
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}
12use vars @EXPORT_OK;
13
14use Class::Template qw(struct);
15struct 'Net::protoent' => [
16 name => '$',
17 aliases => '@',
18 proto => '$',
19];
20
21sub 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
30sub getprotoent ( ) { populate(CORE::getprotoent()) }
31sub getprotobyname ($) { populate(CORE::getprotobyname(shift)) }
32sub getprotobynumber ($) { populate(CORE::getprotobynumber(shift)) }
33
34sub getproto ($;$) {
35 no strict 'refs';
36 return &{'getprotoby' . ($_[0]=~/^\d+$/ ? 'number' : 'name')}(@_);
37}
38
391;
40
41__END__
42
43=head1 NAME
44
45Net::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
60This module's default exports override the core getprotoent(),
61getprotobyname(), and getnetbyport() functions, replacing them with
62versions that return "Net::protoent" objects. They take default
63second arguments of "tcp". This object has methods that return the
64similarly named structure field name from the C's protoent structure
65from F<netdb.h>; namely name, aliases, and proto. The aliases method
66returns an array reference, the rest scalars.
67
68You may also import all the structure fields directly into your namespace
69as regular variables using the :FIELDS import tag. (Note that this still
70overrides your core functions.) Access these fields as variables named
71with 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
73regular array variables, so for example C<@{ $proto_obj-E<gt>aliases()
74}> would be simply @p_aliases.
75
76The getproto() function is a simple front-end that forwards a numeric
77argument to getprotobyport(), and the rest to getprotobyname().
78
79To access this functionality without the core overrides,
80pass the C<use> an empty import list, and then access
81function functions with their full qualified names.
82On the other hand, the built-ins are still available
83via the C<CORE::> pseudo-package.
84
85=head1 NOTE
86
87While this class is currently implemented using the Class::Template
88module to build a struct-like class, you shouldn't rely upon this.
89
90=head1 AUTHOR
91
92Tom Christiansen