use Term::ANSIColor qw (color colored colorstrip :constants); my $x = sprintf '%s XYZ', "Hello ".colored("there", "green")." and bye."; print $x; print sprintf("123 %s 456 %s 789\n", colored("hello", "red").sprintf("abc %s xyz", colored("bye", "green") ), $x );