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

Hello, I was wondering if there was a way to color console output in a perl app that is either use on the command-line, or in readline mode (Using Term::Readline::Gnu). Thanks for any help.

- Nick

Replies are listed 'Best First'.
Re: colored output
by borisz (Canon) on Jan 02, 2005 at 13:31 UTC
      Adding to that, there are some nice examples of Term::ANSIColor in the 2004 Advent Calendar: 2nd.
      In Windows, you'll need to load up Win32::Console::ANSI in addition to Term::ANSIColor, since Windows doesn't naturally interpret ANSI console escape sequences. I'd add use if $^O eq 'MSWin32', 'Win32::Console::ANSI'; if I wanted portability.
        That assumes perl 5.6.2 or higher. A more portable solution might be:
        # check whether if.pm is available eval { use if 1, 'if' } ; my $has_if = defined $@ ; if ( $has_if ) { use if $^0 eq 'MSWin32', 'Win32::Console::ANSI' ; } elsif ( $^O eq 'MSWin32' ) { eval { use Win32::Console::ANSI } ; die "$@, stopped" if $@ ; }
        </facetiousness>
        I would only add ( from Win32::Console::ANSI perldoc )
        Windows NT/2000/XP does not support ANSI escape sequences in Win32 Con +sole applications. This module emulates an ANSI console for the script which uses it.
        Update: This indeed does work under Win2K and AS 5.8 Perl and gellyfish proved it to me. I had previously used PPM to download this and then ran the example with NO luck. gellyfish sent me a screenshot proving otherwise, so I downloaded and built this module which worked as advertised! I will be very careful about just assuming that any module installed via PPM is guaranteed to work.