Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Term::ANSIColor is awesome, but.. (need help) (overload and override)

by tye (Sage)
on Mar 30, 2014 at 19:24 UTC ( [id://1080318]=note: print w/replies, xml ) Need Help??


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        

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1080318]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-24 17:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found