From: Xavier Noria Date: Thu, 31 Jul 2003 00:29:13 +0000 (+0200) Subject: added support for overloaded "" X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=60ad8d7737ceae6f6c1fcd764c298b207f0f9a85;p=p5sagit%2Fp5-mst-13.2.git added support for overloaded "" Message-Id: <200307310029.13567.fxn@hashref.com> p4raw-id: //depot/perl@20367 --- diff --git a/lib/Tie/RefHash.pm b/lib/Tie/RefHash.pm index b4485de..373a94b 100644 --- a/lib/Tie/RefHash.pm +++ b/lib/Tie/RefHash.pm @@ -74,6 +74,8 @@ use vars '@ISA'; @ISA = qw(Tie::Hash); use strict; +use overload; # to support objects with overloaded "" + sub TIEHASH { my $c = shift; my $s = []; @@ -87,8 +89,9 @@ sub TIEHASH { sub FETCH { my($s, $k) = @_; if (ref $k) { - if (defined $s->[0]{"$k"}) { - $s->[0]{"$k"}[1]; + my $kstr = overload::StrVal($k); + if (defined $s->[0]{$kstr}) { + $s->[0]{$kstr}[1]; } else { undef; @@ -102,7 +105,7 @@ sub FETCH { sub STORE { my($s, $k, $v) = @_; if (ref $k) { - $s->[0]{"$k"} = [$k, $v]; + $s->[0]{overload::StrVal($k)} = [$k, $v]; } else { $s->[1]{$k} = $v; @@ -112,19 +115,19 @@ sub STORE { sub DELETE { my($s, $k) = @_; - (ref $k) ? delete($s->[0]{"$k"}) : delete($s->[1]{$k}); + (ref $k) ? delete($s->[0]{overload::StrVal($k)}) : delete($s->[1]{$k}); } sub EXISTS { my($s, $k) = @_; - (ref $k) ? exists($s->[0]{"$k"}) : exists($s->[1]{$k}); + (ref $k) ? exists($s->[0]{overload::StrVal($k)}) : exists($s->[1]{$k}); } sub FIRSTKEY { my $s = shift; keys %{$s->[0]}; # reset iterator keys %{$s->[1]}; # reset iterator - $s->[2] = 0; + $s->[2] = 0; # flag for iteration, see NEXTKEY $s->NEXTKEY; } @@ -133,7 +136,7 @@ sub NEXTKEY { my ($k, $v); if (!$s->[2]) { if (($k, $v) = each %{$s->[0]}) { - return $s->[0]{"$k"}[0]; + return $v->[0]; } else { $s->[2] = 1;