This is in contrast to most other high level languages, in which you must explicitly create a given hash or array before you use it. This includes Python, PHP, JavaScript and all the C style languages. (I am referring only loosely to hashes in the given languages.)

I have given, below, a Perl program demonstrating several examples of autovivification. I'm not certain example 1 is considered autovivification by Perl buffs, or just Perl array behaviour. (Edit: Yes, it is. Thanks, Simpleton.) See the main reference at the bottom for a more drawn out explanation and examples set.

Thoughts on autovivification

As a C kind of person, autovivification did not at first make a great deal of sense to me. It took me awhile to understand all of the examples in the article and successfully write my own. It does make sense, though; albeit probably much more so to someone with Perl experience.

My opinion, predictably, is thus: Autovivification is not a Bad Thing or even Good and Wrong; it is a high-level feature that can be used or abused. It can serve an experienced programmer well in accomplishing what would otherwise be a difficult (or lengthy) task. (Operations on trees come to mind.) Just the same, it can be used by a newbie or 1337 |-|@xx0r to write terrible code that cannot be modified or understood. Therefore, it should be understood by knowledgeable Perlcoders, and used with discretion.

I am tempted to give a tutorial on how to use autovivification, but reading through the example program should suffice for anyone who really needs to understand the issue.

Code listing: autoviv.pl

I am not a Perl monger; in fact, most of what I know about Perl I (re)learned over the course of the morning on which I wrote this. Sorry.

Notes:

Forgive a Perlnewbie for noding this; apparently, no one else wanted to do it. (I can understand why. It took me all morning.) Also, this was in fact, a nodeshell challenge of sorts from some time back.

References:

http://www.perlguy.com/articles/autoviv.html - A good summary of AV

http://www.dwam.net/docs/perl/lib/Pod/perlref.html - A very long-winded discussion of Perl references