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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With