Remember what everybody else said, but I thought I'd point that you would have received an informative message if you had used strict, warnings and diagnostics.
I created a 'counter_data.dat' file with the single character '1' in my current directory, tuned the code a tiny bit (see below), then ran the script. Here's what I got.
Filehandle RECORD_NUMBER opened only for input at io.pl line 8, <RECORD_NUMBER>
line 1 (#1)
(W io) You tried to write on a read-only filehandle. If you intended
it to be a read-write filehandle, you needed to open it with "+<" or
"+>" or "+>>" instead of with "<" or nothing. If you intended only to
write the file, use ">" or ">>". See perlfunc/open.
Here is your sample code with the changes I made.
use warnings; use strict; use diagnostics; open (RECORD_NUMBER, "counter_data.dat") or die; my $count = <RECORD_NUMBER>; # Lexical declaration to keep 'strict' ha +ppy. $count = $count + 1; print RECORD_NUMBER "$count"; close (RECORD_NUMBER) or die;
I know it can be annoying to include those use statements in all of your code, but it really is helpful. See Use strict warnings and diagnostics or die for a lot of information on helpful ways to make Perl tell you what it's doing.
In reply to Re: File I/O
by webfiend
in thread File I/O
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |