Update: Version 2.0
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.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__
To create a MyTie, I used the following:
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:#!/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";
------
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |