James Edward Gray II wrote: > > On Jan 5, 2004, at 9:24 AM, R. Joseph Newton wrote: > > > Gary Stainburn wrote: > > > >> sub new { > >> my $this=shift; # allow for CLASS->new() > >> my $class=ref($this) || $this; # or $obj->new(); > > > > The docs that suggested this are in the process of being deprecated. > > It is not a > > good idea to have objects create other objects with new(). > > Joseph is right here, but I hope he meant to use the word 'clone' > instead of 'create' in the line above.
I would disagree strongly if he had used 'clone'. The only way to clone an object is to have access to the object to be copied, so it has to be an object method. On the other hand it's just confusing to create new objects with anything other than a class method, as the data values of any existing object are irrelevant. > >> $blocks{$name}=$self; > > > > Don't do this. You are asking for trouble and lock-ups with a > > construct like > > this. > > There are perfectly legit reasons to use this kind of construct. The > Flyweight Design Pattern pops into my head immediately. The book > Object Oriented Perl covers this well, if you're interested. Class methods should be self-contained and shouldn't mess around with any data that isn't accessible through the object or class. This is the reverse of what Flyweight does, which is to add a pointer to duplicate data to the object: $self->{flyweight} = \%Flyweight_Data; > >> Now when I wish to DESTROY the hash, I need to free the memory used > >> by the > >> hash, including the two arrays _Links and _Signals. > >> > >> Am I right in thinking that the arrays, along with the scalars will > >> be deleted > >> by the garbage collector when the references in the hash are deleted? > > > > Yes, but circular references, such as the one I pointed out, will gum > > up Destroy > > as well as, most likely, regular functioning. > > >> Am I also right in thinking that this will happen if I lose the > >> reference in > >> $self by simply running: > >> > >> $self=undef; > > > > First you will have to free the circular reference. > > I'm not sure what you're saying here Joseph, but I don't think it's > accurate. DESTROY() is the traditionally accepted place to do > something like break a circular reference and it will work just fine. > When the last reference to the object is gone, DESTROY() will be called > as part of its garbage collection process. If you break the contained > circular references at this point, you can handle them before they > become unreachable. DESTROY is definitely not the place to break circular references. They need to be broken precisely because they contribute to the object's reference count and DESTROY will never be called implicitly until that count falls to zero. Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>