Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: Quick and dirty counter

by CharlesClarkson (Curate)
on Jan 21, 2002 at 13:51 UTC ( #140357=note: print w/ replies, xml ) Need Help??

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;
$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.

Charles K. Clarkson

Comment on Re: Quick and dirty counter
Select or Download Code

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://140357]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (9)
As of 2015-11-25 21:49 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (691 votes), past polls