in reply to Re: When DOESN'T "Use of uninitialized value" show up?
in thread When DOESN'T "Use of uninitialized value" show up?

Autovivification only happens when required

Be careful with such statements:

$ perl -le 'my $a; defined $a->{b}{c}; print ref $a' HASH

Autovivification is not required here, but still the hash access autovivifies $a to { b => {} }.

Replies are listed 'Best First'.
Re^3: When DOESN'T "Use of uninitialized value" show up?
by GrandFather (Saint) on Dec 16, 2011 at 22:44 UTC

    Actually that was exactly the case I had in mind when I made the statement. "b => {}" is required so that the presence of {c} can be checked. In fact I updated the OP's sample to include exists $x->{notdef}{notdeftoo} to demonstrate this case.

    True laziness is hard work
      "b => {}" is required so that the presence of {c} can be checked.

      And that's the part where I disagree. If $a->{b} doesn't exist, we know that $a->{b}{c} cannot exist, and perl could know too. The fact that such reads autovivify are really only due to a quirk in the implementation, and not deducible from a general rule like "autovivification happens only when it needs to".

        And that's the part where I disagree. If $a->{b} doesn't exist, we know that $a->{b}{c} cannot exist,

        But he isn't checking if $a->{b} exists, so it's possible that $a->{b}{c} can exist. Perl is being asked if $a->{b}{c} exists, and it's being done a few steps after autovivifying $a->{b}.

Re^3: When DOESN'T "Use of uninitialized value" show up?
by Skeeve (Parson) on Dec 16, 2011 at 22:49 UTC
    But that's what Grandfather said: " However if a chain of references is required all the intermediate references will autovivify, but not the last item in the chain " didn't he?

    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e