home *** CD-ROM | disk | FTP | other *** search
/ Netrunner 2004 October / NETRUNNER0410.ISO / regular / ActivePerl-5.8.4.810-MSWin32-x86.msi / _c6800dcb167c06bb9244cea7ef8dcd2c < prev    next >
Text File  |  2004-06-01  |  3KB  |  97 lines

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