in reply to trim the blank lines...regex

$str =~ s/^\s*\n//mg; or $str =~ s/^\n//mg depending on your definition of a blank line.

Perhaps a hangup here is that s/^$// will match a blank line but will not remove the ending newline since $ doesn't include the newline.