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.


In reply to Re: Scalar ref acting as simulatenous hash and array? by dragonchild
in thread Scalar ref acting as simulatenous hash and array? by Masem

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.