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.


In reply to Re: Tied HASH as boolean?! by hossman
in thread Tied HASH as boolean?! by gmpassos

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.