Thank you, but it seems that it's for 'print', not for 'die'. How do I
handle with color when I use 'die'?
Exactly the same. Again I warn you, this is not at all portable and
may or may not work on your ANSI-enabled terminal and will not work
on your ANSI-ignorant terminal:
============ tear here ===============
#!/usr/bin/perl
unless ( 1 == 0 ) { die ("\e[1;41;37m Gosh numbers still work\e[0m\n") }
============ tear here ================
The point of the module is to determine the terminal capabilities
and alter the string accordingly. In a non-ANSI terminal, the string
will be printed as you see it above. The ANSI codes do not contribute
to clarity in a non-ANSI terminal. I have no idea how or whether the
module can be made to work with die, but if your script is always going
to run in an environment you are certain of, that's how you get color.
Okay, okay, you made me read the man page. Here is the real way
to do it:
============ tear here ==========================
#!/usr/bin/perl
use Term::ANSIColor;
$boldred = color('bold red');
$reset = color('reset');
unless ( 1 == 0 ) { die ("${boldred}Gosh numbers still work$reset") }
============= tear here ==========================
Evidently you cannot set attributes on the fly with die, but you
can save them as variables, for use as needed.