in reply to Question: Is undef a valid argument?

Consider that the code

#! /usr/bin/perl use strict; use warnings; print scalar @ARGV, "\n"

outputs 0. By explicitly accessing the non-existent array element, perl extends the array and populates those new values with undefs, and that is what gets passed to your subroutine. This is a manifestation of autovivification.. As ikegami points out, this isn't autovivification, but it is returning an undef as you attempt to access the undefined array element.

Replies are listed 'Best First'.
Re^2: Question: Is undef a valid argument?
by ikegami (Patriarch) on May 06, 2009 at 20:37 UTC

    By explicitly accessing the non-existent array element, perl extends the array and populates those new values with undefs,

    No, simply accessing non-existent array elements doesn't extend the array.

    $ perl -le'my @a; print 0+@a; my @x = $a[0]; print 0+@a;' 0 0

    The array is only extended if the element is used as an l-value.

    $ perl -le'my @a; print 0+@a; \$a[0]; print 0+@a;' 0 1 $ perl -le'my @a; print 0+@a; 1 for $a[0]; print 0+@a;' 0 1

    But not every time it's used as an l-value.

    $ perl -le'my @a; print 0+@a; sub {}->( $a[0] ); print 0+@a;' 0 0

    This is a manifestation of autovivification.

    Yes and no.

    The extension of an array or hash can be considered autovivification, but the term usually refers to the creation of new variables by dereferences. Specifically, the document to which you linked only discusses the creation of new variables by deferences, so it's not a useful link.