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 @$self/e; *{ref($self) . '::set_' . $name} = eval $code; ## create the (unitialized) attribute itself push @$self, undef; } }