NaSe77 has asked for the wisdom of the Perl Monks concerning the following question:
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"} }; }
gets me the following output :#! /usr/bin/perl -w use strict; use warnings; use diagnostics; use Person; my $me = new Person; $me->name("Andy"); $me->DESTROY;
Andy dies now: life was nice Andy dies now: life was nice
----
NaSe
:x
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(MeowChow) Re: Writing a REAL destructor
by MeowChow (Vicar) on Jun 05, 2002 at 07:59 UTC | |
by Dog and Pony (Priest) on Jun 05, 2002 at 08:34 UTC | |
by Aristotle (Chancellor) on Jun 05, 2002 at 09:07 UTC | |
by Abigail-II (Bishop) on Jun 05, 2002 at 14:58 UTC | |
by Aristotle (Chancellor) on Jun 05, 2002 at 16:35 UTC | |
| |
by MeowChow (Vicar) on Jun 05, 2002 at 08:47 UTC | |
by NaSe77 (Monk) on Jun 05, 2002 at 09:10 UTC | |
by Abigail-II (Bishop) on Jun 05, 2002 at 15:03 UTC |