Hello and welcome!

I can't really comment on good books for CGI programming as I only do a little of it, but try "CGI Programming with Perl" by Guelich, Gundavaram, and Birznieks. I like it and find it useful. However, being new to Perl, you may want to start with the Camel book or "The Perl Cookbook" by Christiansen and Torkington. All are O'Reilly books.

It sounds like you're asking how to "think Perl". That may take a while, but it sounds like you're going in the right direction: modify existing code. Here's how I might code the routine you're referring to:

sub how_many_lines { open I, 'users.dat' or warn("Couldn't open users.dat\n"), return; # That single statment will try to open "users.dat" # (it better be in the current directory). If that # fails, it'll print "Couldn't open users.dat" and # return undef (the undefined value) to the calling # routine. Yes, it's very terse, but that's part of # the power of Perl. my $lines; # Declare $lines local to this scope (the sub) ++$lines while <I>; # Read the entire file a line at a time, incrementing $lines # every time. close I; # Close the file handle $lines; # return the count to the caller. }
To answer your specific question, yes, there is an EOF test in Perl, but it's very rarely useful. The <> operator returns the undefined value at end-of-file, so an explicit test is usually redundant.

HTH, and good luck on your journey.


In reply to Re: Count number of lines? by VSarkiss
in thread Count number of lines? by ginocapelli

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.