home *** CD-ROM | disk | FTP | other *** search
/ CLIX - Fazer Clix Custa Nix / CLIX-CD.cdr / mac / lib / Net / protoent.pm < prev    next >
Text File  |  1997-05-18  |  3KB  |  95 lines

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