I know $. shows the line number when $/ is set to "\n".
I wanted to emulate the Unix tail command in Perl and print the last 10 lines from a file but $. didn't work. If the file contains 14 lines it starts from 15 in the next loop.
#!/usr/bin/perl
use strict;
use warnings;
my $i;
open my $fh, '<', $ARGV[0] or die "unable to open file $ARGV[0] :$! \n";
do { local $.; $i = $. } while (<$fh>);
seek $fh, 0, 0;
if ($i > 10) {
$i = $i - 10;
print "$i \n";
while (<$fh>) {
#local $.;# tried doesn't work
#undef $.; #tried doesn't work
print "$. $_" if ($. > $i);
}
}
else {
print "$_" while (<$fh>);
}
close($fh);
I want to reset $. so it can be used usefully in next loop.
Using
localwith$.does something else than you think:$.is not read-only, it can be assigned to normally.