in reply to Re^2: Creating an array of hash references
in thread Creating an array of hash references

update: As pointed out below by Tanktalus, the information in this reply is basically wrong. Please ignore it.

I think what's wrong there is that you're pushing the same reference to the same hash over and over onto your array, and also clearing and refilling that hash on each iteration.

What you probably want to do instead is push a reference to an anonymous copy of "%event":

while(1) { my %event = (); eval { %event = SECRET_PACKAGE_NAME::WaitForEvent($timeout); }; if($@) { # timeout occurred, serial burst complete last; } else { push (@Events, { %event }); } }# end while ...
(I could be wrong, but I think this is worth a try.)

Replies are listed 'Best First'.
Re^4: Creating an array of hash references
by Tanktalus (Canon) on Mar 19, 2005 at 00:20 UTC

    Nope - each time through the while loop, you're getting a new %event - each one gets its own address allocated. If the "my" were outside of the while loop, you'd be right. As it is, all your change accomplishes is to copy the hash around an extra time (into an anonymous reference) more than is required.

    (If there is no reference to the %event, the perl VM may optimise it away and just refill the same one, but since the OP is taking a reference to it which lasts through the end of the loop, perl cannot make that optimisation, and allocates a new one.)