You don't want to use DATA as a user file handle. It's reserved for inline data after __END__, __DATA__, or Ctrl-Z.

I'll call the file handle $fh. Reading line by line,

while (<$fh>) {
convert all clusters of whitespace to a single space,
s/\s+/ /g;
get rid of initial whitespace to handle "\n\t" and such,
s/$\s//;
and print the lowercased line,
print lc; }
That's it. Each of those operations takes advantage of $_ as the default argument.

With that method, there's no need to do any special accounting of lines or to store any of your work as you go. If you do need to store the lines for some other purpose, you only need to push the output of lc onto an array where the print occurs.

If you just want a line count, you can immediately follow the while block with,

my $linecount = $. ;
$. is a running linecount of the most recently accessed read handle. It will be volatile in an application with several read handles, hence the assignment to a user variable.

After Compline,
Zaxo


In reply to Re: calculating lines by Zaxo
in thread calculating lines by Yoda_Oz

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.