Autovivification
Encyclopedia
Autovivification is a distinguishing feature of the Perl
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...

 programming language involving the dynamic creation of data structures. Autovivification is the automatic creation of a variable reference when an undefined value is dereferenced. In other words, Perl autovivification allows a programmer to refer to a structured variable, and arbitrary sub-elements of that structured variable, without expressly declaring the existence of the variable and its complete structure beforehand.

In contrast, other programming languages either: 1) require a programmer to expressly declare an entire variable structure before using or referring to any part of it; or 2) require a programmer to declare a part of a variable structure before referring to any part of it; or 3) create an assignment to a part of a variable before referring, assigning to or composing an expression that refers to any part of it.

Perl autovivication can be contrasted against languages such as Python
Python (programming language)
Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...

, PHP
PHP
PHP is a general-purpose server-side scripting language originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document...

, Ruby, JavaScript
JavaScript
JavaScript is a prototype-based scripting language that is dynamic, weakly typed and has first-class functions. It is a multi-paradigm language, supporting object-oriented, imperative, and functional programming styles....

 and all the C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

style languages.

Hashes

The debugger session below illustrates autovivification of a hash:

DB<1> $h{A}{B}{C}{D}=1
DB<2> x \%h
0 HASH(0x83c71ac)
'A' => HASH(0x837d50c)
'B' => HASH(0x83c71e8)
'C' => HASH(0x83c7218)
'D' => 1
DB<3>

Hashes several layers deep were created automatically without any declarations. Autovivification can prevent excessive typing. If Perl did not support autovivification, the structure above would have to be created as follows:

DB<1> %h = (A => {B => {C => {D => 1}}})
DB<2> x \%h
0 HASH(0x83caba4)
'A' => HASH(0x83cfc28)
'B' => HASH(0x83cab74)
'C' => HASH(0x83b6110
'D' => 1
DB<3>

File and Directory Handles

Perl 5.6.1 and newer support autovivification of file and directory handles. Calling open on an undefined variable will set it to a filehandle. According to perl561delta, "[t]his largely eliminates the need for typeglobs when opening filehandles that must be passed around, as in the following example:

for my $file ( qw(this.conf that.conf) ) {
my $fin = open_or_throw('<', $file);
process_conf( $fin );
# no close needed
}
use Carp;
sub open_or_throw {
my ($mode, $filename) = @_;
open my $h, $mode, $filename
or croak "Could not open '$filename': $!";
return $h;
}

Python

Python's collections module contains a defaultdict. These can be used to implement autovivificious dictionaries.

from collections import defaultdict
def hash: return defaultdict(hash)
lupin = hash
lupin["express"][3] = "stand and deliver"

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK