http://www.perlmonks.org?node_id=247369

Most of us are familiar with the term 'flyweight objects' in Perl OO speak, which refers to objects where the blessed thingy is a scalar that is the index into something (a hash or array) containing the object's attributes. This container is lexically scoped to a bare block in the package implementing the object's class, thus providing fairly strong encapsulation.

One way of subverting this encapsulation is a basic 'identity switch' job. A flyweight object does not carry its own attributes around, but it does carry an index around. This index can be modified to take on the identity of some other object in the same class. One way of getting around this is simply making the index harder to guess by randomizing it.

While idling away at the keyboard tonight, I came up with what might be another way of strenghtening the encapsulation in flyweight objects: instead of having the object carrying an index around, have the object be its own index:

package Camel; { my %camels; sub new { my $class=shift; my %args=@_; my $self={}; bless $self, $class; my $key=qq/$self/; $camels{$key}={ %args }; return $self; } } 1;
As you see, the index into the attribute container is a stringified version of the blessed reference itself. The blessed reference is just an empty anonymous hash, so there's no longer an index to play around with. The object is empty - to get at the attributes, you would have to write accessor methods inside the scoping block of package Camel. Encapsulation!

Am I missing something here? Can anybody poke some holes in this? And if not, can anybody come up with a nice name for this little gimmick ;) ?

CU
Robartes-