in reply to Re^9: Ter::ANSIColor is awesome, but.. (need help)
in thread Term::ANSIColor is awesome, but.. (need help)

Haha, dammit!

So... what I want is to always stringify my String::Colored object as a colorless string... unless when it's being printed on the screen.

Is there any way to know that a text is being displayed on the screen? There's no "printing on screen" operator unfortunately ;-)

print() indicates that we're in string context, which forces the stringification. So nothing will help my object know whether it's being printed or used as a hash key.

That's annoying... I'll keep it in mind, in case I come up with a workaround. Otherwise I'll have to either abandon colors, think about re-organizing my code, or write colored() everywhere. I've got time to think...

If only there was a "printing on screen" context...
Or is that a silly idea?

Thanks a lot for your help Corion :-)

  • Comment on Re^10: Ter::ANSIColor is awesome, but.. (need help)

Replies are listed 'Best First'.
Re^11: Ter::ANSIColor is awesome, but.. (need help)
by Anonymous Monk on Mar 31, 2014 at 07:09 UTC
    Well, if overloading stringification calls a function (method, whatever), caller will return name/file/line of who is calling .... so yeah, caller tells you what file on what line, and you can peek at the source to see if its a call to print .... hopefully that rings some alarms for you :)

      This sounds dangerous indeed. I think I'll go with tye's proposal of simulating a "print context" by using a class variable (or attribute).