in reply to Re^2: Question on Reading a file inside while and counting no.of lines
in thread Question on Reading a file inside while and counting no.of lines

It starts at zero...

Quoth perlvar (emphases added): Each filehandle in Perl counts the number of lines that have been read from it. ... When a line is read from a filehandle (via "readline()" or "<>") ... $. becomes an alias to the line counter for that filehandle.

So I would say davido's assertion is not without justification:  $. starts out undefined (which is very like zero) and happily becomes 1 by aliasing when the first line is read. I assume the internal filehandle line counter is 0 or undefined prior to any read on the handle. So there.

>perl -wMstrict -le "my $filename = 'text'; open my $fh, '<', $filename or die qq{opening '$filename': $!}; ;; print qq{\$. initially: }, defined $. ? qq{'$.'} : 'undefined'; ;; while (<$fh>) { chomp; print qq{$.: '$_'}; } ;; close $fh or die qq{closing '$filename': $!}; " $. initially: undefined 1: 'now is the time' 2: 'foo bar baz' 3: 'how now brown cow' 4: 'four score and seven'

Replies are listed 'Best First'.
Re^4: Question on Reading a file inside while and counting no.of lines
by Jim (Curate) on Jun 17, 2013 at 14:56 UTC