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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.