in reply to Removing overloaded string operator

print overload::StrVal $frooble;

Examine what is said, not who speaks.
Silence betokens consent.
Love the truth but pardon error.

Replies are listed 'Best First'.
Re^2: Removing overloaded string operator
by Grundle (Scribe) on Feb 01, 2005 at 22:35 UTC
    An interesting property about this line of code that I noticed is if you do the following

    print overload::StrVal $froob."";

    it will take you back to square one printing out the value instead of the desired Frooble=Hash()

    Although this is minor I thought it would be relevant to this discussion, because as some point some bonehead (me) is bound to do it and claim that overload isn't working properly :p
      Actually, when you look at it it isn't that surprising. Most likely the function overload::StrVal doesn't have a prototype. Therefore when you call it like that Perl parses it as a list operator (Update: The anonymous reply below is correct - the type of operator doesn't matter). That is to say, it is equivalent to
      print overload::StrVal($froob . "");
      In other words, its taking the string value of $froob (which happens to be the return value of an overloaded stringification), appending a blank to it, and then calling StrVal, which does nothing since its argument is an ordinary string.
        The prototype doesn't matter. The concatenation operator has a higher precedence than both list operators (most function calls), and named unary operators (function calls with the $ or ;$ prototypes).