Hi guys,

I recently came across a weird error. It happened a few times. Seems very random, but usually tend to happen around a specific time. Below is the code:

my $file_name = 'abc.txt'; # abc.txt is just one line simple txt file like "Hello world!". It's c +reated by another program. The second the other program creates it, t +he code below tries to read it. # I know the following code is not the right thing to do. I would do i +t open (my $FILE, '<', $file_name) for reading. But it's legacy code +and how the error is produced. So I just show it the way it is now. open(FILE, $file_name) or die "Can't open file $file_name $!\n"); my $line = <FILE>; print $line;

The random error is, it's able to open the file (the die action never happens), but unable to read any data ($line is uninitialized)

If I rerun it after a couple minutes, it runs fine and $line does get the data

My best guess is that the bug is due to legacy "open(FILE, $file_name)" code. Because it did not specify "<" read mode, after it opens the file successfully, another program somehow gets able to write it. Then this program was not able to read it. Is that a fair guess? Anybody has better idea how to fix it? Thanks!


In reply to Able to open file, unable to read by feiiiiiiiiiii

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.