in reply to Unexpected exists() behavior

Yes, that is correct behavior, try using defined and/or length instead. (see update instead)

UPDATE: actually ... those will not work the way you think they will either. Usually one only needs to test for the value of a key, not the key itself. Try greping the keys from the hash, or use a foreach on the keys to see if the key in question is there or not.

Furthermore, try checking only if (exists($List{c}) ) ... exists is not autovivifying the key "c" ... checking $List{c}->{1} is what is causing the key "c" to be created.

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re^2: Unexpected exists() behavior
by tsmoyers (Pilgrim) on Jun 22, 2006 at 16:27 UTC
    Testing for hash{priKey} before the full test is my workaround solution already in the works. It just seemed odd to me that a function with the sole purpose of testing would create an entry under the correct circumstances. Any idea when / why that would be desired behavior?

        That doesn't technically count as autovivification, though; that's undef being treated as 0 in a numeric context. Autovivification is the automatic creation of a reference to a new anonymous hash (or array) when required to implement a multilevel data structure.</pedant>

        Update: And to out pedant myself, you don't necessarily have to have a multilevel structure; my $aref; push @{ $aref }, 1; is autovivification as well. The Uri article referenced from the node I mentioned below uses the phrasing automatically creating a reference of the appropriate type whenever a scalar containing undef is dereferenced. Better than what I used above.

        Further update: The parent did have code using $hash{ $_ }++ and calling that autovivification. It's been removed so this now will make less sense. Ah well.