Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get colored output with printf and Perl's Term::ANSIColor?

is there a way to get with printf colored output?

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

printf "%4.4s\n", colored( '0123456789', 'magenta' );

Output: (only newline)

like image 728
sid_com Avatar asked Mar 15 '10 07:03

sid_com


People also ask

How do I print a color text in Perl?

If you want to use colors in print do the folowing: use Term::ANSIColor qw(:constants); And then use the specific colors names. For example: If you want to print text in green bold color, use: print GREEN BOLD "Passed", RESET; .


2 Answers

I assume you want something like the following:

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

print colored( sprintf("%4.4s", '0123456789'), 'magenta' ), "\n";
like image 184
hlovdal Avatar answered Oct 23 '22 08:10

hlovdal


You need to change your code like the following

printf "%s\n", colored( '0123456789', 'magenta' );

Because we can't get the first 4 character in the string. If you give the string value to the printf function it will print the value up to null character. We can't get the first 4 characters.

like image 44
muruga Avatar answered Oct 23 '22 06:10

muruga