http://qs1969.pair.com?node_id=602707

Having class attributes with an inside out class is pretty straight forward. Just use a regular lexical variable next to the hashes you use to hold your instance attributes. Without some extra logic, however, those variables will hold their values even after the last instance of your class has disappeared.

Say one of those class variables holds a large data structure that you don't want hanging around in memory when it's not needed. You want to have a class attribute that gets garbage collected when the last instance is destroyed.

In my first implementation, each class attribute is held as a reference, and each instance has a copy of that one reference. When the last instance is destroyed, the last reference disappears, and that mammoth data structure gets destroyed too.

use Class::Std; my %example_array_of :ATTR; my %example_hash_of :ATTR; my %example_scalar_of :ATTR; sub BUILD { my ($self, $ident, $args_ref) = @_; my ($other_ident) = keys %example_hash_of; if ( $other_ident ) { $example_array_of{$ident} = $example_array_of{$other_ident}; $example_hash_of{$ident} = $example_hash_of{$other_ident}; $example_scalar_of{$ident} = $example_scalar_of{$other_ident}; } else { $example_array_of{$ident} = []; $example_hash_of{$ident} = {}; $example_scalar_of{$ident} = \do{ my $x = undef }; } } sub get_example_array { return @{$example_array_of{ident shift}}; } sub set_example_array { my $self = shift; @{$example_array_of{ident $self}} = @_; }

What's good: All the work is done at construction, when you are probably doing some initialization work anyway.

What's bad: The initialization is a clutter. The accessors are expensive because of the dereferencing.

An alternate method is to count references in a separate class attribute—a method that is not really specific to inside out objects at all. When the destructor detects that it is destroying the last instance, it undefs each of the class attributes.

use Class::Std; my @example_array; my %example_hash; my $example_scalar; my $instance_count = 0; sub BUILD { my ($self, $ident, $args_ref) = @_; $instance_count++; } sub DEMOLISH { my ( $self, $ident ) = @_; if ( ! --$instance_count ) { undef %example_hash; undef @example_array; undef $example_scalar; } } sub get_example_array { return @example_array; } sub set_example_array { shift; @example_array = @_; }

What's good: It's fast, clean, and simple to understand.

What's bad: There's an extra variable that you can't get rid of but also isn't part of the core functionality.

I've offered two possibilities here. What do the other monks do in this situation?