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