Jean Jean - 2 months ago 9
Perl Question

Perl sprintf formatting of colored output

How do I generate fixed width output in this statement ?

use Term::ANSIColor;

doesn't have any effect in this statement. Are there any color conscious format specifiers ?

mob mob

Sure it does, it's just that the codes that change the color on your terminal also have a width, and so the string you pass to the first sprintf call is already more than 8 characters. Try it with

sprintf("[%18s]", ...

and you'll see an affect.

But the width of terminal codes is esoteric, so you're better off moving the color coding outside of the fixed-width formattng.

printf("[%s]", colored( sprintf("%8s", sprintf("\$%0.2f",$Price) ),'red') )