in reply to (MeowChow) Re: Writing a REAL destructor
in thread Writing a REAL destructor

i see thanks i knew tat perl uses ref-counting but now its more clear and i think i can use that ...
i wanted to have some kind of counter which says how many instances of my class there are (therefor the variable $Census) and since some of my intances will die during my programm i thought to do it this way would be a good idea

i still wonder if there is a way to do this some kind in sub DESTROY though

thanks for the condulence andy was really a good guy ...

----
NaSe
:x

  • Comment on Re: (MeowChow) Re: Writing a REAL destructor

Replies are listed 'Best First'.
Re: Writing a REAL destructor
by Abigail-II (Bishop) on Jun 05, 2002 at 15:03 UTC
    Keeping track of the number of instances can very well be done via DESTROY. I wouldn't put in a reference in the object though. Then you make the count accessable outside your class, and you probably don't want to do that.

    Use something like:

    package MyClass; my $count = 0; sub new { my $proto = shift; my $class = ref $proto || $proto; $count ++; my $obj = ....; bless $obj => $class; } sub DESTROY { $count --; }
    And do not call DESTROY yourself. Perl will do that for you.

    Abigail