Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl color specifiers with redirected output

Tags:

linux

bash

perl

I have a Perl script that uses Term::ANSIColor. It used to be the case that if I redirect the output to a file > file.txt then the file contains just the text, and not the color codes ^[[0m

Something changed on my machine, Ubuntu 10.04, such that redirected output includes these special characters that specify color.

Any idea how to fix this? Can I detect output redirection from inside the perl script and skip the color part?

Thanks!

like image 657
Mikhail Avatar asked Feb 09 '12 18:02

Mikhail


1 Answers

You can test whether you're running interactively using the IO::Interactive package:

use IO::Interactive qw(is_interactive);

if (is_interactive())
{
    # show some fancy color
}

The rationale behind using IO::Interactive (instead of just testing if STDIN is a tty with the -t operator) is extensively explained by Damian Conway.

like image 176
Edward Thomson Avatar answered Sep 19 '22 18:09

Edward Thomson