Since you're already reading the entire input at once, read it all into a scalar with something like
    my $input = do { local $/;  <FILE>; };
and then

c:\@Work\Perl\monks>perl -wMstrict -le "my $input = qq{line1\nline2\n\nline4\n\n\nline7\nline8\n}; print qq{[[$input]]}; ;; $input =~ s{ ^ \s* \n }{}xmsg; print qq{<<$input>>}; " [[line1 line2 line4 line7 line8 ]] <<line1 line2 line4 line7 line8 >>

Update 1: Or, if you need to read all the lines of the file as separate lines and keep them that way, try
    my @lines = grep !m{ \A \s* \Z }xms, <FILE>;

c:\@Work\Perl\monks>perl -wMstrict -le "my @lines = ( qq{line1\n}, qq{line2\n}, qq{\n}, qq{line4\n}, qq{ \t\n}, qq{\t \t \n}, qq{line7\n}, qq{line8\n}, ); print qq{[[@lines]]}; ;; @lines = grep !m{ \A \s* \Z }xms, @lines; print qq{<<@lines>>}; " [[line1 line2 line4 line7 line8 ]] <<line1 line2 line4 line7 line8 >>

Update 2: Slight formatting fix to code in Update 1; no functional change.

Update 3: Rather than using
    !m{ \A \s* \Z }xms
to grep lines in the code in Update 1, I think I'd prefer
    m{ \S }xms
as used by Marshall here; don't know why I didn't use it in the first place. Note that no logical negation is needed.


Give a man a fish:  <%-{-{-{-<


In reply to Re: replace multiple newline characters by AnomalousMonk
in thread replace multiple newline characters by g_speran

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.