in reply to hash refferences

Assigning undef to a hash doesn't "destroy" the hash. I t just empties it.
my %h = (1, 2); print bless(\%h), $/; %h = undef; print bless(\%h), $/; __END__ main=HASH(0x99ae63c) main=HASH(0x99ae63c)

It's the same memory address after assigning undef.

The usual way to do it is to declare the hash inside the loop:

for $val (@variables) { my %temp; # work with %temp here; push @array, \%temp; }

That way a new variable is generated for each iteration.

Replies are listed 'Best First'.
Re^2: hash refferences
by leonidlm (Pilgrim) on Aug 21, 2008 at 12:36 UTC
    But whats the difference between the following:
    1. Declaration of the %temp inside the loop
    2. Performing: undef %temp at the end of each iteration
    Someone ?

      They are completely different. undef %temp empties the variable but still keeps the variable there, while a lexical variable goes out of scope. The undef %temp acts on the values, while leaving the scope acts on the binding of the name to the value.

      What Corion said is right.

      %temp is actually a container - think of a bucket. You can fill this container (putting water into the bucket), and empty it, which is what %temp = undef does.

      Or you can but your bucket into the cupboard, (pushing it to an array), and take a new bucket (declaring %temp inside the loop).

      In this real-world analogy you can see that there's a huge difference between these two things.

        which is what %temp = undef does. No, thats what undef %temp does, big difference
Re^2: hash refferences
by leonidlm (Pilgrim) on Aug 21, 2008 at 12:55 UTC
    Another thing: Unfortunately I can't release the %temp variable every loop iteration, I need to perform it on demand. How I can do it ?
      push @array, { %temp };

      That actually copies the items from %temp into an anonymous hash, and returns its reference.

      See perlreftut and perlref for more details.

        Heh thanks, this is exactly what I did, but somehow it seems not efficient enough.
        What about the following solution:
        Substitute the %temp variable by $tempRef= {} ?

      You can't. You can store copies of the values in another variable maybe.