in reply to Problem pushing hashref results
davorg is correct. You are reusing the same hash each time through the loop. Your debug statement printed the correct values in the hash each time, but what you didn't see is that the hashref itself was the same, as the following example shows. (Note: I changed the while loop to a for loop.)
use strict; use warnings; { print "This is the original approach, reusing \$attrHash\n"; my $result = []; my $attrHash = {}; for( 1..5 ) { print $attrHash, "\n"; push( @$result, $attrHash ); } } { print "This uses a new hash each time through the loop\n"; my $result = []; for( 1..5 ) { my $attrHash = {}; print $attrHash, "\n"; push( @$result, $attrHash ); } } __END__ This is the original approach, reusing $attrHash HASH(0x2254c8) HASH(0x2254c8) HASH(0x2254c8) HASH(0x2254c8) HASH(0x2254c8) This uses a new hash each time through the loop HASH(0x225504) HASH(0x225534) HASH(0x225324) HASH(0x2254e0) HASH(0x183c1d0)
If you declare $attrHash within the loop you'll get a new reference each time.
|
|---|