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.

162 lines
3.1 KiB

  1. package Tie::RefHash;
  2. =head1 NAME
  3. Tie::RefHash - use references as hash keys
  4. =head1 SYNOPSIS
  5. require 5.004;
  6. use Tie::RefHash;
  7. tie HASHVARIABLE, 'Tie::RefHash', LIST;
  8. tie HASHVARIABLE, 'Tie::RefHash::Nestable', LIST;
  9. untie HASHVARIABLE;
  10. =head1 DESCRIPTION
  11. This module provides the ability to use references as hash keys if you
  12. first C<tie> the hash variable to this module. Normally, only the
  13. keys of the tied hash itself are preserved as references; to use
  14. references as keys in hashes-of-hashes, use Tie::RefHash::Nestable,
  15. included as part of Tie::RefHash.
  16. It is implemented using the standard perl TIEHASH interface. Please
  17. see the C<tie> entry in perlfunc(1) and perltie(1) for more information.
  18. The Nestable version works by looking for hash references being stored
  19. and converting them to tied hashes so that they too can have
  20. references as keys. This will happen without warning whenever you
  21. store a reference to one of your own hashes in the tied hash.
  22. =head1 EXAMPLE
  23. use Tie::RefHash;
  24. tie %h, 'Tie::RefHash';
  25. $a = [];
  26. $b = {};
  27. $c = \*main;
  28. $d = \"gunk";
  29. $e = sub { 'foo' };
  30. %h = ($a => 1, $b => 2, $c => 3, $d => 4, $e => 5);
  31. $a->[0] = 'foo';
  32. $b->{foo} = 'bar';
  33. for (keys %h) {
  34. print ref($_), "\n";
  35. }
  36. tie %h, 'Tie::RefHash::Nestable';
  37. $h{$a}->{$b} = 1;
  38. for (keys %h, keys %{$h{$a}}) {
  39. print ref($_), "\n";
  40. }
  41. =head1 AUTHOR
  42. Gurusamy Sarathy gsar@activestate.com
  43. =head1 VERSION
  44. Version 1.3 8 Apr 2001
  45. =head1 SEE ALSO
  46. perl(1), perlfunc(1), perltie(1)
  47. =cut
  48. use v5.6.0;
  49. use Tie::Hash;
  50. use strict;
  51. our @ISA = qw(Tie::Hash);
  52. our $VERSION = '1.3';
  53. sub TIEHASH {
  54. my $c = shift;
  55. my $s = [];
  56. bless $s, $c;
  57. while (@_) {
  58. $s->STORE(shift, shift);
  59. }
  60. return $s;
  61. }
  62. sub FETCH {
  63. my($s, $k) = @_;
  64. if (ref $k) {
  65. if (defined $s->[0]{"$k"}) {
  66. $s->[0]{"$k"}[1];
  67. }
  68. else {
  69. undef;
  70. }
  71. }
  72. else {
  73. $s->[1]{$k};
  74. }
  75. }
  76. sub STORE {
  77. my($s, $k, $v) = @_;
  78. if (ref $k) {
  79. $s->[0]{"$k"} = [$k, $v];
  80. }
  81. else {
  82. $s->[1]{$k} = $v;
  83. }
  84. $v;
  85. }
  86. sub DELETE {
  87. my($s, $k) = @_;
  88. (ref $k) ? delete($s->[0]{"$k"}) : delete($s->[1]{$k});
  89. }
  90. sub EXISTS {
  91. my($s, $k) = @_;
  92. (ref $k) ? exists($s->[0]{"$k"}) : exists($s->[1]{$k});
  93. }
  94. sub FIRSTKEY {
  95. my $s = shift;
  96. keys %{$s->[0]}; # reset iterator
  97. keys %{$s->[1]}; # reset iterator
  98. $s->[2] = 0;
  99. $s->NEXTKEY;
  100. }
  101. sub NEXTKEY {
  102. my $s = shift;
  103. my ($k, $v);
  104. if (!$s->[2]) {
  105. if (($k, $v) = each %{$s->[0]}) {
  106. return $s->[0]{"$k"}[0];
  107. }
  108. else {
  109. $s->[2] = 1;
  110. }
  111. }
  112. return each %{$s->[1]};
  113. }
  114. sub CLEAR {
  115. my $s = shift;
  116. $s->[2] = 0;
  117. %{$s->[0]} = ();
  118. %{$s->[1]} = ();
  119. }
  120. package Tie::RefHash::Nestable;
  121. our @ISA = qw(Tie::RefHash);
  122. sub STORE {
  123. my($s, $k, $v) = @_;
  124. if (ref($v) eq 'HASH' and not tied %$v) {
  125. my @elems = %$v;
  126. tie %$v, ref($s), @elems;
  127. }
  128. $s->SUPER::STORE($k, $v);
  129. }
  130. 1;