Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-26 06:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found