Mikhail
Mikhail

Reputation: 9007

Perl color specifiers with redirected output

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!

Upvotes: 5

Views: 423

Answers (1)

Edward Thomson
Edward Thomson

Reputation: 78743

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.

Upvotes: 6

Related Questions