in reply to adding newline to middle of a string cleanly

I have posted a one liner to do text wrapping earlier, which you could give it a try.
$text =~ s/(.{30,40}(?<=\s\b))/$1\n/mg;
Also I highly recommend the module Text::Autoformat from CPAN that does a great job at text wrapping, justification, etc.
use Text::Autoformat qw(autoformat); $newtext = autoformat($text, { left=>1, right=>40, all=>1 });