in reply to File Truncation Problem

If you are really just trying to match newlines, then why not:
$buf =~ m/(\n*)$/s;
Further, you don't seem to be using the submatch at all, so you could drop the parens:
$buf =~ m/\n*$/s;
But I'm just doing this by sight. I haven't tested to be sure.