Alok Ranjan
Alok Ranjan

Reputation: 131

Print console logs in color using perl

I am using below to print text in color at terminal

use Term::ANSIColor;
print color("red"), "Error\n";

But I am getting output as:

←[31mError

Is there anything else I need to do to print text in color or any other way?

Upvotes: 2

Views: 876

Answers (1)

mmccaff
mmccaff

Reputation: 1281

Are you on a Windows OS? From http://search.cpan.org/~rra/Term-ANSIColor-4.03/lib/Term/ANSIColor.pm -

This module will not work as expected on displays that do not honor these escape sequences, such as cmd.exe, 4nt.exe, and command.com under either Windows NT or Windows 2000. They may just be ignored, or they may display as an ESC character followed by some apparent garbage.

Your output shows that the escape sequence is being added, but the console/terminal cannot display them.

Loading this module before Term::ANSIColor might help: http://search.cpan.org/~jlmorel/Win32-Console-ANSI-1.10/lib/Win32/Console/ANSI.pm

Also, a relevant discussion on perlmonks: http://www.perlmonks.org/?node_id=429607

Upvotes: 3

Related Questions