6 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
8 @EXPORT = qw(getprotobyname getprotobynumber getprotoent);
9 @EXPORT_OK = qw( $p_name @p_aliases $p_proto );
10 %EXPORT_TAGS = ( FIELDS => [ @EXPORT_OK, @EXPORT ] );
14 use Class::Template qw(struct);
15 struct 'Net::protoent' => [
24 $p_name = $pob->[0] = $_[0];
25 @p_aliases = @{ $pob->[1] } = split ' ', $_[1];
26 $p_proto = $pob->[2] = $_[2];
30 sub getprotoent ( ) { populate(CORE::getprotoent()) }
31 sub getprotobyname ($) { populate(CORE::getprotobyname(shift)) }
32 sub getprotobynumber ($) { populate(CORE::getprotobynumber(shift)) }
36 return &{'getprotoby' . ($_[0]=~/^\d+$/ ? 'number' : 'name')}(@_);
45 Net::protoent - by-name interface to Perl's built-in getproto*() functions
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}";
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";
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.
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.
76 The getproto() function is a simple front-end that forwards a numeric
77 argument to getprotobyport(), and the rest to getprotobyname().
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.
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.