rastoboy has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Brothers,

I'm writing a kind of "meta script" to run a whole bunch of other scripts, each of which seems to be by a different author and using different styles of output. I plan to remedy that, but in the short term I thought it would be incredibly useful if I could simply cause each script's STERR output to be a nice bright red, so the operator can more easily pick out problems from that stream of unordered output.

It sounds kindof trivial, but I find I am stumped. I'm using a "standard" gnome-terminal in Ubuntu. Any input would be greatly appreciated!

Replies are listed 'Best First'.
Re: color STERR red?
by Khen1950fx (Canon) on May 09, 2011 at 03:37 UTC
    PerlIO::via::ANSIColor will do it for you.
    #!/usr/bin/perl use strict; use warnings; use PerlIO::via::ANSIColor color => 'red'; binmode STDERR, ':via(ANSIColor)'; warn "This output's colored\n";
      This can't be used if you're also using Capture::Tiny. It will fail with "Couldn't seek on capture handle for stderr".
Re: color STERR red?
by moritz (Cardinal) on May 09, 2011 at 06:03 UTC

    How do you launch them?

    If you do it with require or do, the IO layers mentioned in the replies above will work.

    If you launch them as separate processes (system, fork + exec, backticks) then you'll have to capture STDERR, color it and re-emit it.

    For that you might want to use something like IPC::Run with a callback for handling STDERR:

    use Term::ANSIColor; sub handle_err { print STDERR color('red'), @_; } use IPC::Run; run ['yourprogram', 'argument'], \*STDOUT, \&handle_err;

      That works unless STDOUT uses the same stream as STDERR, in which case all printed code after the STDERR prints are also red.

      You should use

      print STDERR color ("red"), @_, color ("reset");

      or use the colored ("red", string) approach.

      If you need more than just colors, like indents or modification, you can use Text::OutputFilter as an alternative:

      use strict; use warnings; use Text::OutputFilter; use Term::ANSIColor; tie *STDERR, "Text::OutputFilter", 0, *STDERR, sub { color ("red")."$_[0]".color ("reset") }; print STDOUT "Normal\n"; print STDERR "Red?\n"; print STDOUT "Normal again?\n";

      Enjoy, Have FUN! H.Merijn
        Thank you all, lots of great suggestions! /me ponders...
Re: color STERR red?
by LanX (Saint) on May 09, 2011 at 00:02 UTC