Object::Destroyer

Object::Destroyer can make objects with circular references DESTROY normally.
Download

Object::Destroyer Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Adam Kennedy and Igor Gariev
  • Publisher web site:
  • http://search.cpan.org/~adamk/

Object::Destroyer Tags


Object::Destroyer Description

Object::Destroyer can make objects with circular references DESTROY normally. Object::Destroyer can make objects with circular references DESTROY normally.SYNOPSIS use Object::Destroyer; ## Use a standalone destroyer to release something ## when it falls out of scope BLOCK: { my $tree = HTML::TreeBuilder->new_from_file('somefile.html'); my $sentry = Object::Destroyer->new( $tree, 'delete' ); ## Here you can safely die, return, call last BLOCK or next BLOCK. ## The tree will be deleted automatically } ## Use it to break circular references { my $var; $var = $var; my $sentry = Object::Destroyer->new( sub {undef $var} ); ## No more memory leaks! ## $var will be released when $sentry leaves the block } ## Destroyer can be used as a nearly transparent wrapper ## that will pass on method calls normally. { my $Mess = Big::Custy::Mess->new; print $Mess->hello; } package Big::Crusty::Mess; sub new { my $self = bless {}, shift; $self->populate; return Object::Destroyer->new( $self, 'release' ); } sub hello { "Hello World!" } sub release { ...actual code to clean-up the memory... }One of the biggest problem with working with large, nested object trees is implementing a way for a child node to see its parent. The easiest way to do this is to add a reference to the child back to its parent.This results in a "circular" reference, where A refers to B refers to A. Unfortunately, the garbage collector perl uses during runtime is not capable of knowing whether or not something ELSE is referring to these circular references.In practical terms, this means that object trees in lexically scoped variable ( e.g. my $Object = Tree->new ) will not be cleaned up when they fall out of scope, like normal variables. This results in a memory leak for the life of the process, which is a bad thing when using mod_perl or other processes that live for a long time.Object::Destroyer allows for the creation of "Destroy" handles. The handle is "attached" to the circular relationship, but is not a part of it. When the destroy handle falls out of scope, it will be cleaned up correctly, and while being cleaned up, it will also force the data structure it is attached to to be destroyed as well. Object::Destroyer can call a specified release method on an object (or method DESTROY by default). Alternatively, it can execute an arbitrary user code passed to constructor as a code reference. Requirements: · Perl


Object::Destroyer Related Software