Autovivification by foreach

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!


given the following code:

use strict;
use warnings;

my (@a,$ref);
push @a,@$ref;

Perl throws an exception:
Can't use an undefined value as an ARRAY reference
Seems plausible.

We are modifying the code:

my (@a,$ref);
push @a,$_ for @$ref;

Now, there is no exception anymore. Perl creates the array on the fly.
Why is it so? What is the the difference?

Dipl.-Inform. Frank Seitz; /
Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel

Site Timeline