in reply to remove blank lines with regex

Try adding the s modifier to the regex match in the while loop. Also you might want to use $/ instead of a literal \n as it's more portable. Although this code should do the job without having to loop through the string.
$lines =~ s{$/$/}{$/}gs;

HTH

_________
broquaint