use Text::Autoformat qw(autoformat); $newtext = autoformat($text, { left=>1, right=>40, all=>1 });