Leaked source code of windows server 2003
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.

160 lines
4.1 KiB

  1. package Tie::Hash;
  2. =head1 NAME
  3. Tie::Hash, Tie::StdHash - base class definitions for tied hashes
  4. =head1 SYNOPSIS
  5. package NewHash;
  6. require Tie::Hash;
  7. @ISA = (Tie::Hash);
  8. sub DELETE { ... } # Provides needed method
  9. sub CLEAR { ... } # Overrides inherited method
  10. package NewStdHash;
  11. require Tie::Hash;
  12. @ISA = (Tie::StdHash);
  13. # All methods provided by default, define only those needing overrides
  14. sub DELETE { ... }
  15. package main;
  16. tie %new_hash, 'NewHash';
  17. tie %new_std_hash, 'NewStdHash';
  18. =head1 DESCRIPTION
  19. This module provides some skeletal methods for hash-tying classes. See
  20. L<perltie> for a list of the functions required in order to tie a hash
  21. to a package. The basic B<Tie::Hash> package provides a C<new> method, as well
  22. as methods C<TIEHASH>, C<EXISTS> and C<CLEAR>. The B<Tie::StdHash> package
  23. provides most methods required for hashes in L<perltie>. It inherits from
  24. B<Tie::Hash>, and causes tied hashes to behave exactly like standard hashes,
  25. allowing for selective overloading of methods. The C<new> method is provided
  26. as grandfathering in the case a class forgets to include a C<TIEHASH> method.
  27. For developers wishing to write their own tied hashes, the required methods
  28. are briefly defined below. See the L<perltie> section for more detailed
  29. descriptive, as well as example code:
  30. =over
  31. =item TIEHASH classname, LIST
  32. The method invoked by the command C<tie %hash, classname>. Associates a new
  33. hash instance with the specified class. C<LIST> would represent additional
  34. arguments (along the lines of L<AnyDBM_File> and compatriots) needed to
  35. complete the association.
  36. =item STORE this, key, value
  37. Store datum I<value> into I<key> for the tied hash I<this>.
  38. =item FETCH this, key
  39. Retrieve the datum in I<key> for the tied hash I<this>.
  40. =item FIRSTKEY this
  41. Return the (key, value) pair for the first key in the hash.
  42. =item NEXTKEY this, lastkey
  43. Return the next key for the hash.
  44. =item EXISTS this, key
  45. Verify that I<key> exists with the tied hash I<this>.
  46. The B<Tie::Hash> implementation is a stub that simply croaks.
  47. =item DELETE this, key
  48. Delete the key I<key> from the tied hash I<this>.
  49. =item CLEAR this
  50. Clear all values from the tied hash I<this>.
  51. =back
  52. =head1 CAVEATS
  53. The L<perltie> documentation includes a method called C<DESTROY> as
  54. a necessary method for tied hashes. Neither B<Tie::Hash> nor B<Tie::StdHash>
  55. define a default for this method. This is a standard for class packages,
  56. but may be omitted in favor of a simple default.
  57. =head1 MORE INFORMATION
  58. The packages relating to various DBM-related implementations (F<DB_File>,
  59. F<NDBM_File>, etc.) show examples of general tied hashes, as does the
  60. L<Config> module. While these do not utilize B<Tie::Hash>, they serve as
  61. good working examples.
  62. =cut
  63. use Carp;
  64. use warnings::register;
  65. sub new {
  66. my $pkg = shift;
  67. $pkg->TIEHASH(@_);
  68. }
  69. # Grandfather "new"
  70. sub TIEHASH {
  71. my $pkg = shift;
  72. if (defined &{"${pkg}::new"}) {
  73. warnings::warnif("WARNING: calling ${pkg}->new since ${pkg}->TIEHASH is missing");
  74. $pkg->new(@_);
  75. }
  76. else {
  77. croak "$pkg doesn't define a TIEHASH method";
  78. }
  79. }
  80. sub EXISTS {
  81. my $pkg = ref $_[0];
  82. croak "$pkg doesn't define an EXISTS method";
  83. }
  84. sub CLEAR {
  85. my $self = shift;
  86. my $key = $self->FIRSTKEY(@_);
  87. my @keys;
  88. while (defined $key) {
  89. push @keys, $key;
  90. $key = $self->NEXTKEY(@_, $key);
  91. }
  92. foreach $key (@keys) {
  93. $self->DELETE(@_, $key);
  94. }
  95. }
  96. # The Tie::StdHash package implements standard perl hash behaviour.
  97. # It exists to act as a base class for classes which only wish to
  98. # alter some parts of their behaviour.
  99. package Tie::StdHash;
  100. @ISA = qw(Tie::Hash);
  101. sub TIEHASH { bless {}, $_[0] }
  102. sub STORE { $_[0]->{$_[1]} = $_[2] }
  103. sub FETCH { $_[0]->{$_[1]} }
  104. sub FIRSTKEY { my $a = scalar keys %{$_[0]}; each %{$_[0]} }
  105. sub NEXTKEY { each %{$_[0]} }
  106. sub EXISTS { exists $_[0]->{$_[1]} }
  107. sub DELETE { delete $_[0]->{$_[1]} }
  108. sub CLEAR { %{$_[0]} = () }
  109. 1;