1)您可以简单地将ANSI代码包含到heredoc中:
print <<EOD;
XXXX\033[1;30;40m YYYY\033[1;33;43m ZZZZ\033[0mRESET
EOD
Run Code Online (Sandbox Code Playgroud)
2)Heredoc插入变量,因此如果将ANSI颜色包含在变量中,它就可以工作.
my $v="xxxxx";
$var = "\nXXXX\033[1;30;40m YYYY\033[1;33;43mZZZZ\033[0mRESET\n";
print <<EOD;
$var
EOD
Run Code Online (Sandbox Code Playgroud)
3)在#2的基础上,您可以通过Term :: ANSIColor的color()方法生成ANSI代码作为字符串,并使用包含该字符串的变量在heredoc中.对不起,没有工作的例子,因为我没有安装ANSIColor但应该很明显.
您可能希望将特定的ANSI代码存储在某个特定变量中,并将实际文本放在heredoc和sprincle ANSI代码变量中.
您可以使用@{[expression]}定界文档中的语法来评估任意代码。如果您的终端有深色背景和浅色前景色,这个小程序的输出看起来不错:
use Term::ANSIColor;
print <<EOF;
I am using the here doc to print usage messages
for the user. Is there a way to print @{[colored['bright_white'],'specific words']}
BOLD similar to the man pages on unix. I am using
this on Unix. Is there a way to use Term::ANSIColor
(or some other way?) with the here doc?
EOF
Run Code Online (Sandbox Code Playgroud)