in reply to Hashes and boolean values

I thought double quoting "0/1" would result in stringification and not an actual boolean.

"0" produces a string, but that is still false in boolean context.

You should really use exists:

$hash{KEY1} = '1' unless exists $hash{KEY};

Replies are listed 'Best First'.
Re^2: Hashes and boolean values
by osbosb (Monk) on Jan 23, 2012 at 13:24 UTC
    wouldn't that just autovivify it and then burn me later?

      I don't understand, I thought you wanted to set a default. Now you are afraid that setting a default will burn you?

      I am trying to set a hash to a default boolean value unless the hash key exists. Here's an example:

      That's exactly what my code does.

        Don't mind me, I was just having a case of the "Mondays".

        Thanks regardless.

      I cannot answer whether you will burn later, but exists $hash{foo} will not autovivify $hash{foo}. Now, exists $hash{foo}{bar} will autovivify $hash{foo}, but then, so will EXPR if $hash{foo}{bar}.