gmpassos has asked for the wisdom of the Perl Monks concerning the following question:
Take a look in this code, and you can see that the HASH is always FALSE as boolean.
This was test on Perl-5.6.1-Win32 and Perl-5.8.0-Win32, with and without use overload and it always return FALSE!my %hash ; tie(%hash , 'TiedHash') ; print "key: $hash{key}\n" ; if ( %hash ) { print "BOOL: 1\n" ;} else { print "BOOL: 0\n" ;} package TiedHash ; use overload ( 'bool' => sub{ print "OVER1!\n" ; } , ## print() return 1! '""' => sub{ print "OVER2!\n" ; } , '0+' => sub{ print "OVER3!\n" ; } , 'fallback' => 1 , ); sub TIEHASH { bless({}, __PACKAGE__ ) ;} sub FETCH { return( 'fetch_val' ) ;}
Looking for a way to return TRUE for a tied HASH, I found that it looks for the value of the HASH not tied. Soo, the code that work is:
In other words, the HASH need to have at least one KEY before TIE it, or the HASH will be FALSE as boolean!my %hash = (1) ; tie(%hash , 'TiedHash') ; if ( %hash ) { print "BOOL: 1\n" ;} else { print "BOOL: 0\n" ;} package TiedHash ; sub TIEHASH { bless({}, __PACKAGE__ ) ;}
I don't know if this is documented, but haven't found anything about it. I also think that it looks like a bug, since the value of the HASH as boolean (and string) need to be controled and changed (now is like a constant after tie it). The right way is to control this through overload or from some sub inside the TiedHash package (like FETCH).
Well, for me I just need to always have the HASH as TRUE, since I tie it only when I have keys. But in the future this can be changed in Perl, and my code wont work! I holp that I have helped some monks that had this problem too. ;-P
Graciliano M. P.
"The creativity is the expression of the liberty".
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Tied HASH as boolean?!
by hossman (Prior) on Jun 08, 2003 at 03:21 UTC | |
|
Re: Tied HASH as boolean?!
by sauoq (Abbot) on Jun 08, 2003 at 03:16 UTC | |
by gmpassos (Priest) on Jun 08, 2003 at 17:51 UTC | |
by sauoq (Abbot) on Jun 08, 2003 at 19:43 UTC | |
|
Re: Tied HASH as boolean?!
by adrianh (Chancellor) on Jun 08, 2003 at 19:31 UTC | |
|
Re: Tied HASH as boolean?!
by mojotoad (Monsignor) on Jun 09, 2003 at 19:56 UTC | |
by gmpassos (Priest) on Jun 10, 2003 at 02:00 UTC |