Another issue has just come to mind. With the object attributes being keyed on "$self", which includes the name of the class, you can no longer bless an object into a sibling class to implement state transitions.
Again, I guess some people would consider this a feature. I quite like the technique myself :-)
To overcome you'll need to strip out the class name. Assuming nobody changes the output format of references in later perl versions something like this should work.
package Foo;
use strict;
use warnings;
use overload;
my %foo = ();
my %self = (); # we access self enough for it to be worth caching
sub self {
my $self = overload::StrVal shift;
return $self{$self} if exists $self{$self};
$self{$self} = substr($self, index($self, '=')+1);
};
sub new {
my $class = shift;
bless [], $class;
};
sub foo {
my $self = shift->self;
$foo{$self} = @_ if @_;
$foo{$self};
};
sub DESTROY {
my $self = shift->self;
delete $foo{$self};
undef %self;
};
Also, there is another advantage to Abigail-II's method. Storing the attributes in separate hashes should less expensive in memory - buckets cost less than hashes. Might make a difference if you're creating lots of objects.