$padded = sprintf("%-10s", $string); #or use pack "A10" $padded = s/( +)$/"0" x length $1/e;