Fellow Monks

how do i write a real Dectructor that destroyes the intance and makes the garbage collection when called and not when perl thinks the variable isn't needed anymore.
for intance sticking to the example used in perltoot if i call the methode DESTROY : the methode will be executed but also when the program ends ...
package Person; use strict; use warnings; use diagnostics; my $Census = 0; sub new{ my $proto = shift; my $class = ref($proto) || $proto; my $self = {}; $self->{NAME} = undef; # "private" data $self->{"_CENSUS"} = \$Census; bless ($self, $class); ++ ${ $self->{"_CENSUS"} }; return $self; } sub name { my $self = shift; if (@_) { $self->{NAME} = shift } return $self->{NAME}; } 1; # so the require or use succeeds sub DESTROY { my $self = shift; print $self->{NAME}." dies now: life was nice\n"; -- ${ $self->{"_CENSUS"} }; }
#! /usr/bin/perl -w use strict; use warnings; use diagnostics; use Person; my $me = new Person; $me->name("Andy"); $me->DESTROY;
gets me the following output :
Andy dies now: life was nice Andy dies now: life was nice

----
NaSe
:x


In reply to Writing a REAL destructor by NaSe77

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.