Tweet

How can I print to a terminal in color?

This will only work if the terminal you're using supports color. And be warned that the user may have changed their background color.

Solution

Use a module called Term::ANSIColor. This module makes it very simple to color your terminal output:

    #!/usr/bin/perl
    use strict;
    use warnings;
    use Term::ANSIColor;

    print color("red"), "Red!\n", color("reset");
    print color("blue"), "Blue!\n", color("reset");

    exit 0;

See Also

    perldoc -q 'color'
    perldoc Term::ANSIColor
[Top]