in reply to Tied HASH as boolean?!

I don't have an immediate answer for you, but I can tell you that overload isn't working for you because it doesn't overload the behavior of the hash you tie, it overloads the behavior of the object that the hash is tied to. I.e. the return value of tie().

-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re: Re: Tied HASH as boolean?!
by gmpassos (Priest) on Jun 08, 2003 at 17:51 UTC
    Yes I know that the tie object and the behavior of the HASH are diferent. But what I tell, is that we need to control the value of the HASH as boolean, as we control the values of keys, and one way that should be is through overload. I'm just pointing how it should work, since now I think that can't control that is a bug in tie.

    Graciliano M. P.
    "The creativity is the expression of the liberty".

      I agree that there should be a way to do it but I don't think using overload would be the right way. Either one of the TIE* methods should be used or one should be added to give a hook allowing the programmer to set the value of a hash in scalar context.

      Purely as a possible workaround, if you implement FIRSTKEY, NEXTKEY, and STORE, you can get a more useful truth value by forcing list context on the hash.

      use Tie::Hash; tie my %h, 'Tie::StdHash'; # Check with an empty %h first... print "TRUE 1\n" if ( %h ); # Nope not true. print "TRUE 2\n" if ( ()=%h ); # Not true either. # Now a non-empty %h ... $h{foo} = "bar"; print "TRUE 3\n" if ( %h ); # Nope not true. print "TRUE 4\n" if ( ()=%h ); # Yes!
      Of course, this comes with all sorts of huge caveats. Depending on the size of the hash it might not be realistic at all to do this for performance reasons. Your specific tie implementation might even make it an impossibility. (For instance, your list of keys needs to be finite; if it isn't the program will hang until it dies trying to allocate memory.) So, it's not an ideal solution, but just the same, it might work in many cases.

      It really is too bad that you can't use syntax as simple as if ( %h ) { ... }.

      -sauoq
      "My two cents aren't worth a dime.";