echosilex has asked for the wisdom of the Perl Monks concerning the following question:
I'm trying to read from a small file (hits), containing only an integer, increment it, and then write it back to the file. Unfortunately, what $hits prints ends up being "1318131813181318" after a few consecutive executions, although I have a feeling this is a result of the mode I used in opening HITSW. I wrote two subroutines because &read_hits() always needs to be executed with the script, and &write_hits() only needs to be executed on certain occasions. I would appreciate your thoughts and solutions to my problem. Thanks in advance.
sub write_hits { open (HITSW, "+< $_[0]"); my $read_hits = <HITSW>; print HITSW $read_hits++; close (HITSW); } sub read_hits { open (HITSR,"$_[0]"); my $read_number = <HITSR>; close (HITSR); return $read_number; } &write_hits("hits"); my $hits = &read_hits("hits"); print "$hits visitors";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Reading and incrementing an integer
by IlyaM (Parson) on Jul 31, 2002 at 07:27 UTC | |
|
Re: Reading and incrementing an integer
by Dr. Mu (Hermit) on Jul 31, 2002 at 06:49 UTC | |
|
Re: Reading and incrementing an integer
by Zaxo (Archbishop) on Jul 31, 2002 at 08:06 UTC | |
|
Re: Reading and incrementing an integer
by valdez (Monsignor) on Jul 31, 2002 at 08:04 UTC | |
|
Re: Reading and incrementing an integer
by dws (Chancellor) on Jul 31, 2002 at 06:54 UTC | |
|
Re: Reading and incrementing an integer
by ph0enix (Friar) on Jul 31, 2002 at 06:53 UTC |