Subject: | Wish: overloaded hash dereference operator. |
This is nice to have. Having only a $t object (as in the synopsis), it
would be nice to convert that into a sorted hash too. This is how to do
it:
In Tie::IxHash, add this line to the use list:
use overload '%{}' => \&ObjectToHash;
In Tie::IxHash, add this sub:
sub ObjectToHash {
my($s) = shift;
my %result;
tie(%result, __PACKAGE__);
for (my $i = 0; $i < @{$s->[1]}; $i++) {
$result{$s->[1]->[$i]} = $s->[2]->[$i];
}
return wantarray ? %result : \%result;
}
In other code:
my %sorted_hash = %{$t};
Subject: | diff.txt |
--- IxHash.pm.orig 2006-10-11 16:28:36.000000000 +0200
+++ IxHash.pm 2006-12-13 09:50:46.000000000 +0100
@@ -10,6 +10,7 @@
package Tie::IxHash;
use integer;
+use overload '%{}' => \&ObjectToHash;
require Tie::Hash;
@ISA = qw(Tie::Hash);
@@ -420,6 +421,16 @@
$s->Reorder(sort { $s->FETCH($a) cmp $s->FETCH($b) } $s->Keys)
}
+sub ObjectToHash {
+ my($s) = shift;
+ my %result;
+ tie(%result, __PACKAGE__);
+ for (my $i = 0; $i < @{$s->[1]}; $i++) {
+ $result{$s->[1]->[$i]} = $s->[2]->[$i];
+ }
+ return wantarray ? %result : \%result;
+}
+
1;
__END__