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