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?


In reply to Garbage collected class attributes with inside out classes. by kyle

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.