use strict; my $prev_length = 0; my $threshold = 40; my $first_line = 1; while () { chomp; # s/^\s+//g; # s/\s+$//g; my $curr_length = length($_); if ($prev_length > $threshold && $curr_length > 0) { print " "; } elsif ($first_line) { $first_line = 0; } else { print "\n"; } print "$_"; $prev_length = length($_); } print "\n"; __DATA__ This is a longish line whose linebreak should be removed so that this line runs together with it. The preceding double linebreak should be conserved. This little line and this one should stay separated. #### This is a longish line whose linebreak should be removed so that this line runs together with it. The preceding double linebreak should be conserved. This little line and this one should stay separated.