> -----Original Message-----
> From: Nikola Janceski [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, August 15, 2002 12:44 PM
> To: Beginners (E-mail)
> Subject: undef of nested data structures
> 
> 
> I am wondering how undef works.

perldoc -f undef

> 
> I know that undef will undefine a variable if used like 
> undef($scalar);

Yes.

> I also know that it doesn't actually free up the memory but 
> tells Perl that
> it's now available to be recycled for other data.

Well, you probably should't worry about memory allocation details. Perl
takes care of all those details behind the scenes.

> 
> but what about nested data (ie. hashes of hashes, arrays of 
> arrays, etc.).
> if $ref is a reference to a nested structure (let's say a 
> hash of hashes).
> will - 
> undef $ref;
>  - undefine the enitre nested data structure if no other 
> variables contain
> references to any part of it? (assuming no cyclical references)

undef($ref) undefines the scalar $ref. Now, if whatever $ref was pointing to
had no other references to it, then yes, that data would be freed. But this
is a side effect of the undef(), not something special about undef() itself.
The same thing would happen if you assigned "foo" to $ref, so that $ref no
longer contained a reference to some data. Data is freed when the last
reference to it is removed.

You can think of undef like this:

   undef($scalar);     is the same as:      $scalar = undef;
   undef(@array);      is the same as:      @array = ();
   undef(%hash);       is the same as:      %hash = ();

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to