jeremym has asked for the wisdom of the Perl Monks concerning the following question:

Why doesn't this code generate a warning or error? Expected the attempt to dereference the undef value, $array_ref->[0], would result in an error or at least warning.
use strict; use warnings; my $array_ref = [undef]; if ($array_ref-> [0]->{whatever}){ print "true"; } else { print "false"; }

Replies are listed 'Best First'.
Re: Why nothing from 'strict' or 'warnings' with deref'd undef value?
by ikegami (Patriarch) on Apr 02, 2009 at 22:09 UTC

    ( Please use <c> instead of <pre>. Less work for you, more features for us. )

    Append the following, and you'll see why it's not an error:

    print "$array_ref->[0]\n";

    You asked for a hash reference, so Perl obliged rather that giving an error. It's a very convenient (although sometimes problematic) feature called "autovivification". Solution:

    use strict; use warnings; my $array_ref = [undef]; if ( $array_ref->[0] # <--- && $array_ref->[0]->{whatever} ) { print "true\n"; } else { print "false\n"; }

    Note that the arrow is optional between indexes, so
    $array_ref->[0]->{whatever}
    can be written as
    $array_ref->[0]{whatever}

      Thanks for the <c> tip! I'll be sure to do that from now on.

      This all makes sense and my exact example is even right there in the PerlDoc! Time to read up again or get better at Googling...

      What is a valid use for autovivification outside of an assignment operation? I can certainly see the value of
      $ref->[0]{field} = "value";
      but I don't see how my first example can be very useful without an assignment.

        That very question just came up on p5p, the Perl developers' mailing list. It turns out that some rvalue operations autovivify and some don't. Spoke Nicolas Clark, the pumpking:

        So why are indexing and defined special?
        Are they the only exceptions to the exception?

        No answer was obtained.

        Source: Perl RT 63810

Re: Why nothing from 'strict' or 'warnings' with deref'd undef value?
by ig (Vicar) on Apr 02, 2009 at 22:09 UTC

    This is because of autovivification, which is described in Using References. When you dereference your array element as if it were a reference to a hash, an anonymous hash is created and the array element is set to a reference to that hash. If your array element were not undefined, this would not happen - it would be a hash reference or not and so it would work or you would get an error.

Re: Why nothing from 'strict' or 'warnings' with deref'd undef value?
by jethro (Monsignor) on Apr 02, 2009 at 22:11 UTC

    It is called autovivication and its a feature not a bug. It is sort of a precondition for using perl multidimensional arrays and hashes comfortably. For example you don't need to initalize $a[12] to do $a[12][50]= 4;

    If you don't want it you have to explicitly test for undef. Hint: perl5.10 has a new feature to do this with less typing: '//'

Re: Why nothing from 'strict' or 'warnings' with deref'd undef value?
by ELISHEVA (Prior) on Apr 03, 2009 at 03:46 UTC
    You might enjoy this mediation (captured from the Chatterbox) on the weird world of autovivification, strict, and warnings: You can autovivify that there?.

    Best, beth