in reply to Scalar ref acting as simulatenous hash and array?

Ok. After some experimentation, I've got the following package:

Update: Version 2.0

use strict; package MyTie; my @TIED; sub TIEHASH { my $type = shift; my $index = shift; return undef unless $TIED[$index]; my $self; if ($TIED[$index]) { $self = $TIED[$index]; } else { $TIED[$index] = $self = {}; bless $self, ref($type) || $type; } $self->{HASH} = {}; @{$self->{HASH}}{@_} = @{$self->{ARRAY}}; $self->{ISTIED}{HASH} = 1; return $self; } sub TIEARRAY { my $type = shift; my $index = shift; my $self; if ($TIED[$index]) { $self = $TIED[$index]; } else { $TIED[$index] = $self = {}; bless $self, ref($type) || $type; } $self->{ARRAY} = [ @_ ]; $self->{ISTIED}{ARRAY} = 1; return $self; } sub FETCH { my $self = shift; my $key = shift; return undef unless defined $key; if ($key =~ /\D/) { return undef unless $self->{ISTIED}{HASH}; return $self->{HASH}{$key}; } else { return undef unless $self->{ISTIED}{ARRAY}; return $self->{ARRAY}[$key]; } } sub STORE { my $self = shift; my $key = shift; return undef unless defined $key; if ($key =~ /\D/) { return undef unless $self->{ISTIED}{HASH}; $self->{HASH}{$key} = shift; } else { return undef unless $self->{ISTIED}{ARRAY}; $self->{ARRAY}[$key] = shift; } } sub FETCHSIZE { my $self = shift; return $#{$self->{ARRAY}}; } 1; __END__
It's obviously not complete, as most of the ARRAY and HASH functions aren't included. They're relatively easy to do, according to the model.

To create a MyTie, I used the following:

#!/usr/local/bin/perl use strict; use warnings; use MyTie; my $index = 0; my @blah; tie @blah, "MyTie", $index, (5, 4, 3, 2, 1); my %blah; tie %blah, 'MyTie', $index, qw(zero one two three four); print "$blah[2]\n"; print "$blah{two}\n";
Now, to extend this so you can have more than one MyTie, you have to decide how you're going to construct this. One possibility is to do the following:
  1. Have a package global of a list in MyTie.
  2. Always construct the array first.
  3. Make the first element in the list some $index in that global array.
  4. pop that index off before putting it into $self->{ARRAY}.
  5. When creating the HASH part, put a index => $index.
Yet another thing to add is to create the array first, then create the hash by only passing the keys in as a list and creating the hash on the fly. I suspect this is what you want more. (Done in v2.0)

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.