Without having changed your code too much
It could stand a bit more changing though.
$line =~ s/\n//g;
next if !$line;
Why use s///
and why the /g if you do? If you are going to do it, chomp $line;
is preferable. Now what if the line is "0\n"? Zero is false. Contrived? Okay... then what if the line contains some whitespace before the newline? Whitespace is true. Checking for the truth of !$line
isn't really what you mean. You want to skip $line
unless it contains a non-whitespace character so just say what you mean:
next unless $line =~ /\S/;
No need to change the line and no need to re-append the newline later.
"My two cents aren't worth a dime.";