简体   繁体   English

具有重定向输出的Perl颜色说明符

[英]Perl color specifiers with redirected output

I have a Perl script that uses Term::ANSIColor . 我有一个使用Term::ANSIColor的Perl脚本。 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 过去,如果我将输出重定向到文件> file.txt则该文件仅包含文本,而不包含颜色代码^[[0m

Something changed on my machine, Ubuntu 10.04, such that redirected output includes these special characters that specify color. 我的机器Ubuntu 10.04上发生了一些变化,以便重定向的输出包括这些指定颜色的特殊字符。

Any idea how to fix this? 任何想法如何解决这个问题? Can I detect output redirection from inside the perl script and skip the color part? 我可以从perl脚本中检测输出重定向并跳过颜色部分吗?

Thanks! 谢谢!

You can test whether you're running interactively using the IO::Interactive package: 您可以使用IO :: Interactive包测试是否正在以交互方式运行:

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 . Damian Conway广泛地解释了使用IO :: Interactive的原理(而不只是使用-t运算符测试STDIN是否是tty)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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