--- "Brett W. McCoy" <[EMAIL PROTECTED]> wrote:
> On Tue, 5 Jun 2001, Alan F. Larimer, Jr. wrote:
> 
> > 1) After reading the perltoot manpage and most of the perlobj manpage, I am
> > left with a question of "properness."  Should instance methods always only
> > return references?  In the perltoot manpage it shows an instance method
> > returning an array instead of the reference to it.  Then pushing that onto
> an
> > array (in main) to be used later.  Wouldn't that be less effecient
> > (memory/speed) than just passing back the reference?
> 
> Not necessarily.  You might want to have your instance method do something
> different when used in a list context versus being used in a scalar
> context.  If you used a reference, the user of your class would have to do
> some funny things to go into a list context versus a scalar context.  If
> you let your method handle that, the user of your class will be happier.

Whether it's a scalar, array, etc . . . if you return a reference, the class
user has to dereference it.  So for style's sake, wouldn't you want to keep it
consistent.  Still, there are the memory usage and speed issues.  (I imagine
their really isn't much of an issue, but I'd still like to have an idea.)

> > 2) DESTROY   I'm not sure I understand how this is used.  I do understand
> the
> > purpose.  For example (from perltoot manpage), a class variable is storing
> the
> > poulation of a Person class (increased everytime new is called).  There is
> a
> > DESTROY function/subroutine for accounting for a loss of an object,
> decreasing
> > the population.  Does one using the class have to specifically call the
> > DESTROY, or is that called when the reference to an object is gone?  (In
> case
> > you couldn't tell, it really has me lost.)
> 
> DESTROY should never be called explicitly (although you can) -- it
> functions similarly to C++ destructors.  It is called implicitly when the
> last reference to the object goes away.  In most cases, creating a
> destructor is probably not necessary unless you need to close filehandles,
> socket connections, modify class data (as in your example), etc.

So, for example (from perltoot again) . . . 
I have a Person class that saves (amoung other things) name and peers.  I
create the object and push it onto an array in package main.  When I remove
(pop, shift, or indexing) the object from that main array, the DESTROY is
automatically called and decrements the class variable of population. Correct?

=====
Alan F. Larimer, Jr.
http://www.geocities.com/lrng_lnx/

__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail - only $35 
a year!  http://personal.mail.yahoo.com/

Reply via email to