Well, in this case it was a cargo cultism.  I understand why one may
want to do this, but that was not my intent when I was writing this; I
just grabbed an example constructor.  I should have thought about
the choice to make "new" a function that could be called by both the
class and the object, but instead I grabbed working code.  Let this be a
less to all of the cut and pasters out there:  think about what the code
you are grabbing does, understand why it does it, and know why you are
using it.  I would have felt a lot better about this if I could have
stood up to Merlyn the Great and Powerful (no sarcasm there, He wrote
_Learning Perl_ after all) and boldly said "No! I will not stop using
this because I believe such and such"; instead, the only excuse I have
is a sort of chagrined "Well, I was going fast and didn't want to
think."

However, this example had worse things in it than copy and paste
syndrome.  I made an erroneous statement about DESTROY.  In my email, I
stated that having the deconstructor prevented memory leaks.  THIS IS
FALSE.  The example program I posted had two leaks in it (both $fred and
$barney fail to be GCed).  You must call the deconstructor MANUALLY to
avoid leaks when your variables are going to go out of scope.  The
DESTROY method is only called when the reference count goes down to
zero, and since the reference count never gets down to zero the DESTROY
method never gets called.  The code should have looked like:

<snip href="my last email on the thread Re: Self Def">
$fred->print_info;
$barney->print_info;

$fred->DESTROY;   # clean up memory since these objects could refer 
$barney->DESTROY; # to each other (in fact in this example they do)

package Cartoon;
</snip>

On 19 Jun 2001 15:17:09 -0700, Randal L. Schwartz wrote:
> >>>>> "Chas" == Chas Owens <[EMAIL PROTECTED]> writes:
> 
> Chas> sub new {
> Chas>         my $class = shift;
> Chas>         $class    = ref($class) || $class;
> 
> PLEASE stop doing this.
> 
> I know there's a Very Popular Tutorial included with your
> documentation that says that's a Right Way of doing it,
> but that's not a consensus amongst all people who've been doing
> Perl for a long time, and object programming for Even Longer.
> 
> See <http://www.perlmonks.org/index.pl?node_id=52089> for my longer
> take on this.
> 
> -- 
> Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
> <[EMAIL PROTECTED]> <URL:http://www.stonehenge.com/merlyn/>
> Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
> See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
> 
--
Today is Setting Orange, the 24th day of Confusion in the YOLD 3167
Pzat!


Reply via email to