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