in reply to Term::ANSIColor is awesome, but.. (need help)

Change the module to produce objects with stringification overloaded to return the original string (w/o color escapes). Override the output functions you use (like 'print'), to do something like:

sub ... { local( $Term::ANSIColor::InOutput ) = 1; return CORE::...( @_ ); }

and have the stringification include the color escapes if $InOutput is true.

- tye        

  • Comment on Re: Term::ANSIColor is awesome, but.. (need help) (overload and override)
  • Download Code

Replies are listed 'Best First'.
Re^2: Term::ANSIColor is awesome, but.. (need help) (overload and override)
by mascip (Pilgrim) on Mar 30, 2014 at 21:44 UTC

    I had the very same idea but I was thinking of using a global variable. A class variable - or a class attribute - is much better. Thank you!

    And a good thing is that it should also work for exceptions, if I also create a die_color() function.

    1. If I implement this, is it worth sharing it on CPAN?

    2. Is it "bad practice"?

    3. Is there a better way?

    I really like the idea of a string declared with a color, but who behaves just like a normal string in the program. Only the display is colored.

      I would really like to hear people's feedback: Is this String::Colored module a good / bad idea ?

      Additional idea given by a Monger:

      local *CORE::GLOBAL::print = \&String::Colored::sing;
      That's if I want to not need to change anything in my current code, while adding colors. Any warnings against the dangers of this trick would be very welcome :-)

        That's what "Override the output functions you use (like 'print')" means.

        - tye