Initial bulk commit for "Git on MSys"
[msysgit/historical-msysgit.git] / lib / perl5 / 5.6.1 / Tie / RefHash.pm
blob461148821f590e69a04ef36c9a22ed7ca52e5d06
1 package Tie::RefHash;
3 =head1 NAME
5 Tie::RefHash - use references as hash keys
7 =head1 SYNOPSIS
9 require 5.004;
10 use Tie::RefHash;
11 tie HASHVARIABLE, 'Tie::RefHash', LIST;
12 tie HASHVARIABLE, 'Tie::RefHash::Nestable', LIST;
14 untie HASHVARIABLE;
16 =head1 DESCRIPTION
18 This module provides the ability to use references as hash keys if you
19 first C<tie> the hash variable to this module. Normally, only the
20 keys of the tied hash itself are preserved as references; to use
21 references as keys in hashes-of-hashes, use Tie::RefHash::Nestable,
22 included as part of Tie::RefHash.
24 It is implemented using the standard perl TIEHASH interface. Please
25 see the C<tie> entry in perlfunc(1) and perltie(1) for more information.
27 The Nestable version works by looking for hash references being stored
28 and converting them to tied hashes so that they too can have
29 references as keys. This will happen without warning whenever you
30 store a reference to one of your own hashes in the tied hash.
32 =head1 EXAMPLE
34 use Tie::RefHash;
35 tie %h, 'Tie::RefHash';
36 $a = [];
37 $b = {};
38 $c = \*main;
39 $d = \"gunk";
40 $e = sub { 'foo' };
41 %h = ($a => 1, $b => 2, $c => 3, $d => 4, $e => 5);
42 $a->[0] = 'foo';
43 $b->{foo} = 'bar';
44 for (keys %h) {
45 print ref($_), "\n";
48 tie %h, 'Tie::RefHash::Nestable';
49 $h{$a}->{$b} = 1;
50 for (keys %h, keys %{$h{$a}}) {
51 print ref($_), "\n";
54 =head1 AUTHOR
56 Gurusamy Sarathy gsar@activestate.com
58 =head1 VERSION
60 Version 1.3 8 Apr 2001
62 =head1 SEE ALSO
64 perl(1), perlfunc(1), perltie(1)
66 =cut
68 use v5.6.0;
69 use Tie::Hash;
70 use strict;
72 our @ISA = qw(Tie::Hash);
73 our $VERSION = '1.3';
75 sub TIEHASH {
76 my $c = shift;
77 my $s = [];
78 bless $s, $c;
79 while (@_) {
80 $s->STORE(shift, shift);
82 return $s;
85 sub FETCH {
86 my($s, $k) = @_;
87 if (ref $k) {
88 if (defined $s->[0]{"$k"}) {
89 $s->[0]{"$k"}[1];
91 else {
92 undef;
95 else {
96 $s->[1]{$k};
100 sub STORE {
101 my($s, $k, $v) = @_;
102 if (ref $k) {
103 $s->[0]{"$k"} = [$k, $v];
105 else {
106 $s->[1]{$k} = $v;
111 sub DELETE {
112 my($s, $k) = @_;
113 (ref $k) ? delete($s->[0]{"$k"}) : delete($s->[1]{$k});
116 sub EXISTS {
117 my($s, $k) = @_;
118 (ref $k) ? exists($s->[0]{"$k"}) : exists($s->[1]{$k});
121 sub FIRSTKEY {
122 my $s = shift;
123 keys %{$s->[0]}; # reset iterator
124 keys %{$s->[1]}; # reset iterator
125 $s->[2] = 0;
126 $s->NEXTKEY;
129 sub NEXTKEY {
130 my $s = shift;
131 my ($k, $v);
132 if (!$s->[2]) {
133 if (($k, $v) = each %{$s->[0]}) {
134 return $s->[0]{"$k"}[0];
136 else {
137 $s->[2] = 1;
140 return each %{$s->[1]};
143 sub CLEAR {
144 my $s = shift;
145 $s->[2] = 0;
146 %{$s->[0]} = ();
147 %{$s->[1]} = ();
150 package Tie::RefHash::Nestable;
151 our @ISA = qw(Tie::RefHash);
153 sub STORE {
154 my($s, $k, $v) = @_;
155 if (ref($v) eq 'HASH' and not tied %$v) {
156 my @elems = %$v;
157 tie %$v, ref($s), @elems;
159 $s->SUPER::STORE($k, $v);