in reply to Re^2: hash and array mismatch
in thread hash and array mismatch

It is called "autovivification". Have a read through perlreftut and perlref.

You have seen it happen in a light weight way whenever you assign a value to an array element that hadn't been used before. It is a little more interesting in the case of a reference which you are using for the first time, but is one of the underpinnings of creating and managing "interesting" data structures in Perl, as indeed was the case here.

My pleasure, education is a large part of what PerlMonks is about.


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^4: hash and array mismatch
by TGI (Parson) on May 10, 2007 at 20:19 UTC

    I knew that autovivification would let you do things like $foo{a}{b}{c} = 7; I just didn't know that it worked for push and friends as well. Which is funny, because in perlreftut, it discusses the push related aspect.

    I know I read perlreftut a few years ago. I guess this tidbit failed to stick.

    For reference purposes, you can also do:

    my ($foo, $bar, $baz); $foo->[1] = 92; # $foo = [92] print $bar->[0]; # $bar = []; print @{ $baz }; # DIES - $baz not in lvalue context, no autoviv.

    Thanks for the pointer to autovivification. I obviously didn't understand it properly when I was first learning, and never really came back to the subject. Methinks I need to carefully reread Chapter 8 of Programming Perl.


    TGI says moo