in reply to Truthfulness of references

To my initial surprise, the code above printed "We have a hash reference.", even though the hash I've referenced is empty ( ie: no keys and no values ).

No matter how many keys it hash, it's still a hash. ref(\%hash) returns HASH, which is a true value. if (%$hash_ref) will tell you if the referenced hash is empty or not.

Replies are listed 'Best First'.
Re^2: Truthfulness of references
by njcodewarrior (Pilgrim) on Mar 13, 2007 at 11:15 UTC

    Thanks ikegami.

    So I should use:

    if ( %$hash_ref )

    to check whether the hash is empty or not?

    By the way, thanks for your reply on this. I haven't used the Chatterbox before, so I wasn't sure how to respond ( I've been meaning to read up on it, but haven't found the time ).

    njcodewarrior

      Yes, when $hash_ref holds a reference to a hash.

      If $hash_ref doesn't hold a reference to a hash, you'll get a warning or a runtime error depending on what it holds and whether use strict 'refs'; is active or not.