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


in reply to Quick and dirty counter

A text file should end with a newline. Your code expects it, that's why you are using chomp.

print OUTFILE "pagecounter\n$Counter"; should be:
print OUTFILE "pagecounter\n$Counter\n";

Also, you should check open for success. Generally this is done with die:
open COUNTER, $counter_file or die "Cannot open $counter_file: $!";

Finally, you don't need the while block. If you know the count will always be on the second line:

my $counter_file = 'counter.txt'; open COUNTER, $counter_file or die "Cannot open $counter_file: $!"; my $first_line = <COUNTER>; chomp( my $counter = <COUNTER> ); close COUNTER;
and:
$counter++; open COUNTER, '>', $counter_file or die "Cannot open $counter_file: $! +"; print COUNTER "$first_line$counter\n"; close COUNTER;

Assuming you keep ignoring the warnings merlyn mentions above.




HTH,
Charles K. Clarkson