in reply to Re^2: Neverending Foreach Loop
in thread Neverending Foreach Loop
it creates a new my %hash each time it's called, since the old one went out of scope and no longer exists1. If you want to prove this to yourself, create a trivial sub like that and call it twice, storing both hashrefs. Add some data to one hashref, then look in the other - it will still be empty.sub get_hashref { my %hash; return \%hash; }
(There are ways to have it return references to the same hash every time it's called, such as
but defectdata() apparently doesn't do this.)my %hash; sub get_hashref2 { return \%hash; }
1 Not entirely true, in that the previous hash still exists and can be accessed by the previously-returned hashref, but it has become anonymous and is no longer my %hash. Read up on "closures" for more about how this works (and how it can be used to do some pretty powerful stuff).
|
|---|