Term::ANSIColor 当术语不支持颜色时

Term::ANSIColor when term doesn't support colors

我编写了一个 Perl 程序来为其输出着色。

不幸的是,我还有一个解释器可以运行我的程序并显示其输出,但不了解任何 ANSI 转义序列。

简单绕过Term::ANSIColor的正确方法是什么?对于每个 sayprint 语句,即 say ($ENV{'TERM'} ne 'NONE')?colored('foo', green):'foo';

始终处理这两种情况是很糟糕的

欢迎提出任何建议。

使用ANSI_COLORS_DISABLED环境变量:

ANSI_COLORS_DISABLED

If this environment variable is set to a true value, all of the functions defined by this module (color(), colored(), and all of the constants not previously used in the program) will not output any escape sequences and instead will just return the empty string or pass through the original text as appropriate. This is intended to support easy use of scripts using this module on platforms that don't support ANSI escape sequences.