Google
 
   
Login
Username:

Password:


Lost Password?

Register now!
Search
Main Menu
top books
Polls
What do you think about php-deluxe.net?
Excellent!
Cool
Hmm..not bad
What the hell is this?
encyclopedia
recommendation
compare webbrowser
Freenet DSL
Who's Online
10 user(s) are online (9 user(s) are browsing encyclopedia)

Members: 0
Guests: 10

more...
browser tip
Unix Befehle
manual of unix befehle
recommendation!
Sponsored
partner

Autovivification

Autovivification is a unique feature of the Perl programming language involving the dynamic creation of data structures. Autovivification is the automatic creation of a reference when an undefined value is dereferenced. In effect, this causes access to a nonexistent element of a hash (associative array) or array to create both the hash or array, and all elements below the given index for the array.

This is in contrast to most other high level languages, in which hashes or arrays must be explictly created before using. This includes Python programming language, PHP, Ruby programming language, JavaScript and all the C programming language style languages.

=Hashes=

The debugger session below illustrates autovivification of a hash:

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

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

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

=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:

sub myopen { open my $fh, @_ or die Can t open @_ : $! ; return $fh; } { my $f = myopen(