views:

472

answers:

3

Hello, 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)

+6  A: 

The problem is "%4.4s\n" try "%s\n" it will work. the reason is that colors are chars (escape chars) and you are cutting them. try printf "%s\n", length(colored( '0123456789', 'green' )); to understand better.

+5  A: 

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.

muruga
+12  A: 

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";
hlovdal