in reply to Why does exists cause autovivication?

exists doesn't autovivify.

>perl -le "my %hash; exists($hash{foo}); print scalar %hash" 0

Dereferencing (->) does.

>perl -le "$hash->{foo}; print $hash ? 1 : 0" 1

Replies are listed 'Best First'.
Re^2: Why does exists cause autovivication?
by Argel (Prior) on Dec 29, 2007 at 21:12 UTC
    I was under the impression though that the dereference, autovivication, etc. happens within 'exists'. Assuming that is correct, at the first indication that autovivication is going to occur it could return false (and thus prevent the autovivication). Assuming the Perl 5 code base would easily allow for that.

    Or is my assumption about when the dereferencing and autovivication occurs wrong?

      "Within exists" implies it's done by exists. It's not. Perhaps a better term would be "within an exists context", although there's currently no such thing.

        I can see how you might interpret it that way, but the last sentence in my previous post clarifies that I was talking about when the events occur. I guess I'm just stumbling over what the correct terminology to use is and I apologize for that.

        So, lets try this again. When do the dereferencing and autovivication occur? Do they happen first and then the autovivified version is passed to 'exists'? Or does it happen after the parameter is passed to 'exists'?

        If the former then my entire question was off-base. If the latter, then in the OP I was asking why it was implemented this way.

        I like your idea about an "exists context" -- that certainly seems appropriate. Though I'm guessing that may be hard to implement?

        Anyway, thanks for putting up with my poorly worded questions and thanks for the answers!