Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.

Re: Heisenberg Uncertainty Hash

by japhy (Canon)
on Apr 26, 2005 at 22:48 UTC ( [id://451792]=note: print w/replies, xml ) Need Help??

in reply to Heisenberg Uncertainty Hash

You're being bitten by "autovivification". When you check to see if $hash{d}{value} exists, Perl will autovivify $hash{d} if it doesn't already exist.

Your best solution is to do if ($hash{d} and !$hash{d}{value}) { ... }.

Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

Replies are listed 'Best First'.
Re^2: Heisenberg Uncertainty Hash
by DrHyde (Prior) on Apr 27, 2005 at 08:50 UTC
    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.
      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}) { ... }
      A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://451792]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-06-17 20:59 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.