Perhaps I'm oversimplifying, but wouldn't this work as a base class for array based objects?
package foo; use strict; sub new { bless [], ref $_[0] || $_[0] || __PACKAGE__; } sub declare { my $self = shift; ## auto vivification fails to make the correct indexes $self->[0] = {} unless $self->[0]; no strict 'refs'; foreach my $name (@_) { ## create the index reference $self->[0]{$name} = @$self; ## create a nicely named index variable ${ref($self) . '::' . $name . '_idx'} = @$self; ## create the accessor/mutator methods (my $code = 'sub { $_[0][VALUE] }') =~ s/VALUE/scalar @$self/e +; *{ref($self) . '::get_' . $name} = eval $code; ($code = 'sub { $_[0][VALUE] = $_[1] }') =~ s/VALUE/scalar @$s +elf/e; *{ref($self) . '::set_' . $name} = eval $code; ## create the (unitialized) attribute itself push @$self, undef; } }
It trades space (possibly a lot of space) for time and convenience, but that's often a good trade. I'm sure there's a (perhaps non-trivial) way to move the index references back into package space to save memory, but I'll leave that as an exercise.

In reply to Re: Problems I've had with array based objects by vanishing
in thread Problems I've had with array based objects by jimt

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.