in reply to Why is this not autovivifying?

Autovivification happens in an lvalue context, but you're using it only as an rvalue in the else branch of your accessors.

Update: And just to be (more :) pedantic, this isn't strictly speaking autovivification that you're expecting (that's automatically creating a reference to the proper data type when undef is used in an lvalue context). What you're seeing is access to a non-existent hash slot not creating an entry in that slot.

Replies are listed 'Best First'.
Re^2: Why is this not autovivifying?
by blahblah (Friar) on Apr 02, 2007 at 15:47 UTC
    Ah. Thank you very much Fletch for the excellent and succinct explanation. I'm really disappointed I didn't know that before - and judging from the -1 vote I'm not the only one disappointed that I didn't know that before.

    Thanks!
Re^2: Why is this not autovivifying?
by ikegami (Patriarch) on Apr 03, 2007 at 01:06 UTC

    Not at all. An lvalue context is not required.

    print $hash{foo}{bar}; # Autovivifies $hash{foo}

      Oop, quite right. The presence of 'lvalue' in perlref was still in my mind. It's more like 'the use of an undef value (retrieved from somewhere which could be an lvalue; i.e. $a->{"foo"} will, undef->{"foo"} won't) to the left of a -> (implicit or explicit) attempting to dereference the value'.