I've been picking the camel and my brain for a solution to this, but nothing's been helpful nor apparent.
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";
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.