简体   繁体   中英

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!

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM