DeusVult has asked for the wisdom of the Perl Monks concerning the following question:
And this script worked perfectly well. Each index of the array was unique (as opposed to if I made calls to push @array, %hash; in which case every index of the array contained a reference to the same hash, with whatever values were put in the hash last).my @array; my %hash; &callaFunction(); &callaFunction(); &callaFunction(); foreach my $element ( @array ) { my %localHash = %{$element}; # print %localHash's contents } sub callaFunction { # fill %hash with random info push ( @array, {%hash} ); }
However, when, armed with this newfound knowledge, I sallied forth into my main program and applied what I had learned, I was thoroughly vexed to find that my original problem had once again reared its ugly head! That is to say, although I called
I found that each element of @globalArray had the same values within it. So somehow, although I created anonymous hashes to push onto the array, a single reference to the same hash was pushed onto the array again and again.push ( @globalArray, {%globalHash} );
Monks, I am at a loss. The inner magics of references are but newly known to me. So my question to you, O monks whose wisdom shines like a stream of bat urine on a moonlit night, is this: how could the call to push which I describe above still result in the same reference being pushed onto my array repeatedly?
Some people drink from the fountain of knowledge, others just gargle.
|
|---|