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.

123 lines
1.9 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. untie HASHVARIABLE;
  9. =head1 DESCRIPTION
  10. This module provides the ability to use references as hash keys if
  11. you first C<tie> the hash variable to this module.
  12. It is implemented using the standard perl TIEHASH interface. Please
  13. see the C<tie> entry in perlfunc(1) and perltie(1) for more information.
  14. =head1 EXAMPLE
  15. use Tie::RefHash;
  16. tie %h, 'Tie::RefHash';
  17. $a = [];
  18. $b = {};
  19. $c = \*main;
  20. $d = \"gunk";
  21. $e = sub { 'foo' };
  22. %h = ($a => 1, $b => 2, $c => 3, $d => 4, $e => 5);
  23. $a->[0] = 'foo';
  24. $b->{foo} = 'bar';
  25. for (keys %h) {
  26. print ref($_), "\n";
  27. }
  28. =head1 AUTHOR
  29. Gurusamy Sarathy gsar@umich.edu
  30. =head1 VERSION
  31. Version 1.2 15 Dec 1996
  32. =head1 SEE ALSO
  33. perl(1), perlfunc(1), perltie(1)
  34. =cut
  35. require 5.003_11;
  36. use Tie::Hash;
  37. @ISA = qw(Tie::Hash);
  38. use strict;
  39. sub TIEHASH {
  40. my $c = shift;
  41. my $s = [];
  42. bless $s, $c;
  43. while (@_) {
  44. $s->STORE(shift, shift);
  45. }
  46. return $s;
  47. }
  48. sub FETCH {
  49. my($s, $k) = @_;
  50. (ref $k) ? $s->[0]{"$k"}[1] : $s->[1]{$k};
  51. }
  52. sub STORE {
  53. my($s, $k, $v) = @_;
  54. if (ref $k) {
  55. $s->[0]{"$k"} = [$k, $v];
  56. }
  57. else {
  58. $s->[1]{$k} = $v;
  59. }
  60. $v;
  61. }
  62. sub DELETE {
  63. my($s, $k) = @_;
  64. (ref $k) ? delete($s->[0]{"$k"}) : delete($s->[1]{$k});
  65. }
  66. sub EXISTS {
  67. my($s, $k) = @_;
  68. (ref $k) ? exists($s->[0]{"$k"}) : exists($s->[1]{$k});
  69. }
  70. sub FIRSTKEY {
  71. my $s = shift;
  72. my $a = scalar(keys %{$s->[0]}) + scalar(keys %{$s->[1]});
  73. $s->[2] = 0;
  74. $s->NEXTKEY;
  75. }
  76. sub NEXTKEY {
  77. my $s = shift;
  78. my ($k, $v);
  79. if (!$s->[2]) {
  80. if (($k, $v) = each %{$s->[0]}) {
  81. return $s->[0]{"$k"}[0];
  82. }
  83. else {
  84. $s->[2] = 1;
  85. }
  86. }
  87. return each %{$s->[1]};
  88. }
  89. sub CLEAR {
  90. my $s = shift;
  91. $s->[2] = 0;
  92. %{$s->[0]} = ();
  93. %{$s->[1]} = ();
  94. }
  95. 1;