![]() |
|
Welcome to the Monastery | |
PerlMonks |
Re: Autovivification in perlby pemungkah (Priest) |
on Jan 11, 2014 at 00:17 UTC ( #1070214=note: print w/replies, xml ) | Need Help?? |
"Autivivification" is a fancy word for "don't let me mess up if stuff isn't there yet".
In hashes, autovivification creates the hash entry you were looking for and sets it to undef; if you try to continue down a chain of references to other anonymous data structures, autovivification creates them (empty) and continues. So If you said what logically happens is this: Note that Perl does not actually keep doing and undoing the assignments of undef; it just plows through, building the structures as it goes. That's autovivification. Note that if you just access something at the end of one of these chains, you'll get undef; Perl does set the value to undef if you don't assign it anything, just as it would if you referenced a key in a hash that hadn't been set yet. Also note that this forces autovivification to create all those unused entries in the arrays, so we have 'bad_idea' referencing a 100,000 element anonymous array, the last element of which references a 100,000 element anonymous array, the last element of which references another 100,000 element array, the last element of which is set to our string. Using hashes unless you absolutely have to use arrays will let you simulate a sparse array.
In Section
Seekers of Perl Wisdom
|
|