in reply to Tied HASH as boolean?!
I'm no authority on tied objects, or on overload ... but i think you are confussing the two.
By overloading 'bool' in your package, you can define the behavior of evaluating an object of that package in boolean context -- but I don't think that's what is happening in your code.
By tieing your obejct as a hash, only the TIE* methods for hashes are ever called on your object. When you say: "if (%hash)" it doesn't matter that you have overloaded 'bool' for your package, all that matters is that you are evaluating a hash in a bolean context, which is 'true' if-and-only-if that hash has any elements.
Consider these modifications to your script (and teh output)...
#!/usr/bin/perl -wl use strict; my %hash ; my $obj = new TiedHash(); tie(%hash , 'TiedHash') ; print "checking Hash...."; print 'BOOLHASH' if ( %hash ); print "checking Object...."; print 'BOOLOBJ' if ( $obj ); package TiedHash ; use overload 'bool' => sub{ print "BOOL"; return 1 ; } , '""' => sub{ print "STRING"; return 'tiedhash' ; } , ; sub TIEHASH { bless({}, __PACKAGE__ ) ;} sub new { return TIEHASH; } __END__ checking Hash.... checking Object.... BOOL BOOLOBJ
...I'm not sure what methods on your tied hash are getting called when %hash is evaluated in boolean context, but i'm sure if you override all of the TIE* methods, you can figure it out.
|
|---|