http://www.perlmonks.org?node_id=1014861


in reply to Count number of lines in a text file

Everybody should be aware that in an ASCII file, the last line may NOT contain a '\n' character. This means the following code:
while (sysread FILE, $buffer, 4096) { $lines += ($buffer =~ tr/\n//); }
will never count the last line! Please do not use the above code !!!

Use:

while (<FILE>) { $lines++ }