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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Able to open file, unable to read
by Apero (Scribe) on May 13, 2016 at 20:36 UTC | |
|
Re: Able to open file, unable to read
by stevieb (Canon) on May 13, 2016 at 20:01 UTC | |
|
Re: Able to open file, unable to read
by Marshall (Canon) on May 14, 2016 at 09:09 UTC | |
|
Re: Able to open file, unable to read
by ExReg (Priest) on May 13, 2016 at 20:15 UTC | |
|
Re: Able to open file, unable to read
by Anonymous Monk on May 13, 2016 at 20:16 UTC |