I skipped the bit where you set up the names of input file and output file. But generally speaking, you should always always 'use strict;' and 'use warnings;'. They really are the very best ways to stop a program doing anything weird.

And as mentioned above- a while look is better than a foreach if you're processing a large file. (Makes little odds for a small file, but it's good form).

Perl is very clever - it understand context. <$input_fh> says 'read from $input_fh' but if you do:

my $line = <$input_fh>;

it simply reads the next line. Where if you do

my @whole_file = <$input_fh>;

It will read the whole file into that array - which is in effect what my first snippet does. It doesn't make much difference if you're working with a small file, but the difference will become very important with a 500MB file.

I'd strongly suggest taking time to understand what each line is doing - code that someone on the internet gave you is never trustworthy. (Although on Perlmonks, usually true evil will get stomped upon)


In reply to Re^5: Shorten the headers of a file and remove empty lines using perl by Preceptor
in thread Shorten the headers of a file and remove empty lines using perl by intect

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.