I was thinking the following, which would lead to
undef array elements once
$obj->DESTROY gets called. Still, I like it ;-)
package Outside::In;
use strict;
use warnings;
{
my $obj_counter = 0;
my @name;
sub new {
my $class = shift;
my $instance_index = $obj_counter;
my $self = sub { return $instance_index };
bless $self, $class;
$obj_counter++;
return $self;
}
sub name {
my $self = shift;
$name[$self->()] = shift if @_;
return $name[$self->()];
}
sub DESTROY {
my $self = shift;
delete $name[$self->()];
}
}
package main;
my $obj1 = Outside::In->new;
my $obj2 = Outside::In->new;
$obj1->name('Bob');
$obj2->name('Fred');
print $obj1->name, "\n", $obj2->name, "\n";
print $obj1->();