tags:

views:

240

answers:

2

Hi Everyone.

In perl 5.8.5, if I do the following, I don't get an error:

use strict;

my $a = undef;
foreach my $el (@$a) {
  ...whatever
}

What's going on here? Printing out the output of ref($a) shows that $a changes to become a valid array reference at some point. But I never explicitly set $a to anything.

Seems kind of odd that the contents of a variable could change without me doing anything.

Thoughts, anyone?

EDIT: Yes, I know all about auto-vivification. I always thought that there had to be a assignment somewhere along the way to trigger it, not just a reference.

+13  A: 

Auto-vivification is the word. From the link:

Autovivification is a distinguishing feature of the Perl 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, PHP, Ruby, JavaScript and all the C style languages.

Auto-vivification can be disabled with no autovivification;

Kinopiko
I kept refreshing this page to see the answer cause I was curious. Wow, Perl really IS odd :)
Bartek
It seems natural to me.
Kinopiko
This is Perl's virtue of DWIM (Do what I mean).
spoulson
... unless it bites you when you doesn't like it.
Hynek -Pichi- Vychodil
FYI, you can disable autoviv with http://search.cpan.org/perldoc/autovivification
daotoad
Thanks, daotoad. I have put that into the answer itself.
Kinopiko
+7  A: 

Read Uri Guttman's article on autovivification.

There is nothing odd about it once you know about it and saves a lot of awkwardness.

Perl first evaluates a dereference expression and sees that the current reference value is undefined. It notes the type of dereference (scalar, array or hash) and allocates an anonymous reference of that type. Perl then stores that new reference value where the undefined value was stored. Then the dereference operation in progress is continued. If you do a nested dereference expression, then each level from top to bottom can cause its own autovivication.

Sinan Ünür
Followup question. Why doesn't this auto-vivify: `my $a; my @x = @$a`?
FM
@FM: in general, things that are only reading from a dereference won't autovivify, while things that may modify what's dereferenced do.The for loop is an example of something that may modify (since $el is aliased into the array and the array may be changed through it). This is only a loose rule, though; there are some rough edges.
ysth
be careful of cases with a subscript, even when just reading from them, autovivication will likely take place
Eric Strom