[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]

exists does not autovivify



>Speaking for weirdness, is anyone else concerned with the autovivication
>of intermediate steps of exists()?  My expectation has always been that
>exists() does not autovivify.  Once the behavior is documented, then it
>will be more difficult to fix later.

It *is* already documented.  

    % man 3p exists
    ...

    Note that the EXPR can be arbitrarily complicated as long as
    the final operation is a hash key lookup:

        if (exists $ref->{A}->{B}->{$key})  { }
        if (exists $hash{A}{B}{$key})       { }

    Although the last element will not spring into existence just
    because its existence was tested, intervening ones will. Thus
    `$ref->{"A"}' and `$ref->{"A"}->{"B"}' will spring into existence
    due to the existence test for a $key element. This happens
    anywhere the arrow operator is used, including even

        undef $ref;
        if (exists $ref->{"Some key"})      { }
        print $ref;             # prints HASH(0x80d3d5c)

    This surprising autovivification in what does not at first--or
    even second--glance appear to be an lvalue context may be fixed
    in a future release.

It's not "exists" that is autovivifying.  I can come up with as
many more scores of examples as you'd like, all using other functions
that exists().

    do { 
	say "exists() does not autovivify"
    } until you believe me;

It's the use of the derefencing operation that does this.  And how about
this:

    undef @x;
    print if $x[100][100];
    print $x[100];

I hope you aren't to curse the if() statement now. :-)

--tom


Follow-Ups from:
Joshua N Pritikin <joshua.pritikin@db.com>

[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]