Problems? Is your data what you think it is? | |
PerlMonks |
Re: Explaining Autovivicationby throop (Chaplain) |
on Jun 12, 2008 at 04:20 UTC ( [id://691586]=note: print w/replies, xml ) | Need Help?? |
Autovivification saves a lot of annoying effort. So if $quick is originally empty
creates the intermediate hashes so $quick can point to a hash-of-hashes-of-hashes. But autovivification can bite you / surprise you, too. If you are testing a value (rather than setting it) autovivification can still add entries to your hash. That is, new keys. E.g., if you start with and then you Now when you look at keys %$stateCityPopulation, 'Ohio' is in there. It's holding an empty array. It was created when you were trying to access the value for $stateCityPopulation->{Ohio}->{Springfield}. Even though you got undef back as a value, the itermediate array got created 'by side effect.' If you're debugging code, and you find an array has a bunch of unexpected keys, which hold empty hashes, then autovivification is to blame. If you'd changed the code in the above to You'd have skipped the autovivification (at the expense of messier code.) BTW, if you're debugging a rogue autovivification, the 'w' switch in the debugger is very handy. throop
In Section
Seekers of Perl Wisdom
|
|