http://www.perlmonks.org?node_id=197646


in reply to Padding with \s

How about this?

$var .= substr(" " x 40, length($var)) if length($var) < 40;

The if is so that you don't get warnings about undefined vars.

If you want to print:
print $var . (length($var)<40?substr(" " x 40, length($var)):"");

Though I'm sure someone can optimize it a little better.