Gary Stainburn wrote:
>
> I'm working for the first time with object, my $self being a ref to an
> anonymous hash, i.e.
>
> sub new {
>   my $this=shift;                # allow for CLASS->new()
>   my $class=ref($this) || $this; # or $obj->new();
>   my ($name,$type)[EMAIL PROTECTED];
>   my $self={};
>   bless $self,$class;
>   $self->{_Name}=$_[0]; # name of track block section
>   $self->{_Type}=$_[1]; # type of block
>   $self->{_Links}=();  # blocks connected to me
>   $self->{_Signals}=();
>   $blocks{$name}=$self;
>   return $self;
> }
>
>
> 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?
>
> Am I also right in thinking that this will happen if I lose the reference in
> $self by simply running:
>
> $self=undef;

Yes. But why do you think you need to write a DESTROY method? It's only
necessary if the object's destruction must include more than just
deallocating object data (for instance if a running instance count is
being kept and needs decrementing). If the method is defined, Perl will
call DESTROY when it is about to automatically release all of the object
data anyway.

By the way, these lines:

  $self->{_Links}=();  # blocks connected to me
  $self->{_Signals}=();

assign 'undef' to the two hash values. What you mean here is

  $self->{_Links} = [];
  $self->{_Signals} = [];

but it will probably work as it is since

  push @{$self->{_Links}}, $link

still works if the hash value is undefined: Perl 'autovivifies'
an empty anonymous array if it hasn't already been done.

HTH,

Rob



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to