$code =~ s/\n//g; #remove all the newlines $code .= "\n"; #add one to the end #### $code =~ s/\n(?=.)//g # remove all but the last newline