http://qs321.pair.com?node_id=1162984

feiiiiiiiiiii has asked for the wisdom of the Perl Monks concerning the following question:

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!