Tie::Hash::Array - a hash which is internally implemented as a sorted array


  use Tie::Hash::Array;

  tie my %hash, 'Tie::Hash::Array';
  $hash{foo} = 'bar';

  my $object = new Foo;
  $hash{$object} = 'You can also use objects as keys.';

  while ( my($key, $value) = each %hash ) {
      $key->dwim($value) if ref $key && $key->can('dwim');


Hashes tied to this class will interally be stored as an array alternately containing keys and values, with its keys sorted in standard string comparison order, that is, as cmp does.

While the main purpose of this module is serving as a base class for Tie::Hash::Abbrev, some of its side effects may also be useful by themselves:

  • "each" in perlfunc will return the contents in sorted order.

  • You can use objects as keys. (Please note, however, that in this case the string representations of these objects should stay constant, or to be exact, their string sorting order should maintain stable, or else you might get undesired results.)


Please do not rely on the implementation details of this class for now, since they may still be subject to change.

If you'd like to subclass this module, please let me know; perhaps we can agree on some standards then.


None known so far.


        Martin H. Sluka


This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

The full text of the license can be found in the LICENSE file included with this module.