|Do you know where your variables are?|
A couple of years ago I wrote a module that tried to implement the Flyweight pattern in perl, and submitted it for Code Review. I had originally planned to release it to CPAN, but reconsidered after I thought more about it. Looking at the code now, there wasn't much advantage to using my module versus just implementing it in modules that need it. Besides, my approach didn't really provide any real encapsulation of the data since it just returned the full data structure to the caller.
However, I still believe it can be a useful approach if data encapsulation is important to you.
In addition to the points Abigail-II identified (no garbage collection after the object goes out of scope, and inability to handle overload stringification) I'd like to add that you're $self doesn't need to be a blessed hash. Since you're only using the blessed hash's address when it's stringified as your object identifier inside the flyweight hash, you could use any type of blessed variable. In this case I'd recommend a simple blessed scalar, which would have less memory consumption and is cleaner IMHO.
Below is a sample class that deals with all three of the issues. It uses overload's StrVal() function to get the scalar's address, bypassing string overloading. (However, I'd only recommend doing this if you actually are using string overloading, for performance reasons):
Dan Kubb, Perl Programmer
In reply to (dkubb) Re: (1) Encapsulation through stringification - a variation on flyweight objects