Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.9 KiB

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