in reply to Re: Heisenberg Uncertainty Hash
in thread Heisenberg Uncertainty Hash

Instead of just checking the truth or otherwise of $hash{d}{value}, check for existence using the exists() function. That way you won't get weird bugs when the value is (eg) zero or the empty string. You might also want to check that $hash{d} is a reference to a hash before trying to access it.

Replies are listed 'Best First'.
Re^3: Heisenberg Uncertainty Hash
by Anonymous Monk on Apr 27, 2005 at 09:04 UTC
    Putting an exists there, (exists $hash{'d'}->{'value'}) still autovivificates $hash{'d'}. This might be surprising, but it is logical. After all, you are enquiring about the hash %{$hash{'d'}} (asking whether it contains the key 'd'). Since that hash doesn't exists yet, Perl does what it always does in that case: it creates it.

    If you want to cover all your bases, and make sure no hash gets created as a side effect, be more explicite:

    if (exists $hash{'d'} && ref $hash{'d'} eq "HASH" && exists $hash{ +'d'}->{value}) { ... }
      --, obvious