As long as your objects are not referring to each other or creating circular references in other ways, they should be removed when they go out of scope (technically, when their reference count goes to 0).

If you want notices of your objects being destroyed, you can create a DESTROY method in their class:

package MyClass; sub DESTROY { my $self = shift; # assuming object has a "name" property warn "object $self->{name} is being destroyed now"; }

Depending on your problem, creation and destruction of your objects might be a bottleneck (I've hardly ever been in that situation, but still). If so, you could consider "reusing" the same object, or using class methods (if you're only using one instantiation at a time anyway), or getting rid of the object altogether and using hash references or scalars, or array refs instead.

Give Devel::DProf a shot, before you start optimizing. It will probably give you more insight in what part of your code is causing the problem.


In reply to Re: Bless MyObject by Joost
in thread Bless MyObject by perlcapt

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.