package Flyweight; use strict; use overload (); { my %flyweight; sub new { my $self = bless \my $scalar => shift; # do other things to set up the object return $self; } sub get_attribute { my $self = shift; my $value = $flyweight{ overload::StrVal($self) }{attribute}; # do something to $value return $value; } sub set_attribute { my $self = shift; my $value = shift; # throw an exception if $value doesn't meet constraints $flyweight{ overload::StrVal($self) }{attribute} = $value; return; } # clean up the object when it goes out of scope sub DESTROY { delete $flyweight{ overload::StrVal(shift) } } } 1;